我正在為 API 開發(fā)一個庫。當您發(fā)出 curl 命令時,有一個 API 端點 (POST):curl -H "X-API-TOKEN: API-TOKEN" 'http://interest-graph.getprismatic.com/text/topic' \ --data "title=Clojure" \ --data "body=Clojure is a dynamic programming language that targets the Java Virtual Machine (and the CLR, and JavaScript). It is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming.Clojure is a compiled language - it compiles directly to JVM bytecode, yet remains completely dynamic. Every feature supported by Clojure is supported at runtime."我正在嘗試使用 http.NewRequest 執(zhí)行上述命令: var jsonReq = []byte(`{"title": "Clojure", "body": "Clojure is a dynamic programming language that targets the Java Virtual Machine (and the CLR, and JavaScript). It is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming. Clojure is a compiled language - it compiles directly to JVM bytecode, yet remains completely dynamic. Every feature supported by Clojure is supported at runtime."}`) buf := new(bytes.Buffer) err := json.NewEncoder(buf).Encode(jsonReq) if err != nil { fmt.Println(err) } u := "http://interest-graph.getprismatic.com/text/topic" ApiToken := "API-TOKEN" req, err := http.NewRequest("POST", u, buf) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-API-TOKEN", ApiToken) if err != nil { log.Fatal(err) } c := http.DefaultClient resp, err := c.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() r, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } fmt.Println(string(r))不確定我這樣做是否正確,因為我不斷收到錯誤 500。知道我做錯了什么嗎?
Golang:帶有 JSON 負載的 http.NewRequest POST 返回錯誤 500
慕的地6264312
2021-09-13 19:49:27