英文:
Golang: http.NewRequest POST with JSON payload returns error 500
问题
我正在为一个API编写一个库。有一个API端点(POST),当你发出curl命令时,如下所示:
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错误。你知道我做错了什么吗?
英文:
I'm working on a library for an API. There's an API endpoint (POST) which when you issue a curl command is:
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."
I'm trying to perform the above command using 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))
Not sure if I'm doing this right, because I keep getting an error 500. Any idea what I'm doing wrong?
答案1
得分: 7
正确的代码:
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func main() {
u := "http://interest-graph.getprismatic.com/text/topic"
ApiToken := "API-CODE"
data := url.Values{}
data.Set("title", "Clojure")
data.Set("body", `Clojure是一种面向Java虚拟机(以及CLR和JavaScript)的动态编程语言。它被设计为一种通用语言,结合了脚本语言的易用性和交互式开发,以及多线程编程的高效和健壮的基础设施。
Clojure是一种编译语言-它直接编译为JVM字节码,但仍然完全动态。Clojure支持的每个功能都在运行时支持。`)
b := bytes.NewBufferString(data.Encode())
req, err := http.NewRequest("POST", u, b)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
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))
}
英文:
The correct code:
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func main() {
u := "http://interest-graph.getprismatic.com/text/topic"
ApiToken := "API-CODE"
data := url.Values{}
data.Set("title", "Clojure")
data.Set("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.`)
b := bytes.NewBufferString(data.Encode())
req, err := http.NewRequest("POST", u, b)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论