gpt4 book ai didi

go - 使用 header 作为 application/json 在 Golang 中获取 POST 参数

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

我是 golang 的新手,正在尝试使用 httprouter ( https://github.com/julienschmidt/httprouter) 通过 POST 方法创建 REST API。我使用简单的原始请求, header 作为 Content-Type:application/json。

我已经很努力了,但没有找到获取原始查询参数的方法。

req.FormValue("name") 或 req.Form.Get("name") 工作正常,但标题为 Content-Type:application/x-www-form-urlencoded

有没有人试过获取原始查询参数(标题为 Content-Type : application/json)?

最佳答案

使用Json解码:req 是 *http.Request

decoder := json.NewDecoder(req.Body)
decoder.UseNumber()
err := decoder.Decode(&yourStruct)

关于go - 使用 header 作为 application/json 在 Golang 中获取 POST 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51205126/

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