亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang:帶有 JSON 負載的 http.NewRequest POST 返回錯誤 500

Golang:帶有 JSON 負載的 http.NewRequest POST 返回錯誤 500

Go
慕的地6264312 2021-09-13 19:49:27
我正在為 API 開發一個庫。當您發出 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 執行上述命令:   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。知道我做錯了什么嗎?
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 265 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號