gpt4 book ai didi

go - 使用 JSON 的 POST 调用失败

转载 作者:数据小太阳 更新时间:2023-10-29 03:40:37 25 4
gpt4 key购买 nike

<分区>

我有一个接受这样的请求主体的 API:

内容类型是application/json。

{
"firstname": "foo",
"surname": "bar",
"age": 10,
"group":"test"
}

当我使用像 Postman 这样的客户端时,请求会通过。

但是,来自 Go 的相同请求失败了:

    type Student struct {
firstname string
surname string
age int
group string
}
student:= Student{"foo", "bar", 10, "test"}
b, err := json.Marshal(student)

rest := restCall("POST", "http://api", b, "xyz123")

func restCall(method string, url string, body []byte, token string) {

req, _ := http.NewRequest(method, url, bytes.NewReader(body))

req.Header.Add("Authorization", token)
req.Header.Add("content-type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
}

我收到一个内部 HTTP 500 错误,指出:

缺少必需参数“组”。

但是,正如您从我的代码中看到的那样,我确实通过了它。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com