为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
噜噜哒 2023-05-04 17:08:31
我正在使用 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]](当我使用邮递员时我得到了这样的信息)这有什么问题。出站数据供参考
查看完整描述

1 回答

?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

你需要这样的东西:


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'

    }

})

查看完整回答
反对 回复 2023-05-04
  • 1 回答
  • 0 关注
  • 80 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信