Axios 和 fetch 在 Golang 中给出空映射,即使在 url 编码时(添加了标头)

我正在使用 axios 发送 http 请求(我也使用了 fetch 但它给出了相同的结果)。


axios.post("http://localhost:3000/login",

            {

                answer: 42

            },

            {

                headers: {

                    "Content-Type": "application/x-www-form-urlencoded",

                },

            })

在我的 go 文件中,我正在记录响应


func post(req *http.Request, res http.ResponseWriter) {

    req.ParseForm()


    fmt.Println(req.Form)

}

日志如下:


map[{"answer":42}:[]]

但是我希望它如下所示:


map["answer":[42]]

(当我使用邮递员时我得到了这样的信息)


这有什么问题。


出站数据供参考

http://img4.mukewang.com/645376250001728705870313.jpg

噜噜哒
浏览 83回答 1
1回答

慕婉清6462132

你需要这样的东西:var querystring = require('querystring');axios.post('http://localhost:3000/login', querystring.stringify({'answer': 42},headers: {    'Content-Type': 'application/x-www-form-urlencoded'});您可以使用 params 配置选项设置查询字符串参数,它肯定会起作用:axios.post("http://localhost:3000/login", "", {    params: {answer: 42},    headers: {        'Content-Type': 'application/x-www-form-urlencoded'    }})
打开App,查看更多内容
随时随地看视频慕课网APP