使用golang写一个http post请求content-type为applicationjson请求参数为prompt值为 你是谁
以下是使用golang编写的HTTP POST请求,content-type为application/json,请求参数为prompt,值为“你是谁”:
package main
import (
"bytes"
"fmt"
"net/http"
)
func main() {
url := "http://example.com/api"
jsonData := []byte(`{"prompt":"你是谁"}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
}
注意,这里需要将url替换为目标API的URL。如果API需要授权,请在请求头中添加授权标头。
原文地址: https://www.cveoy.top/t/topic/brUE 著作权归作者所有。请勿转载和采集!