gpt4 book ai didi

javascript - 带有 json 正文的 POST 请求 javascript

转载 作者:行者123 更新时间:2023-11-28 10:48:44 25 4
gpt4 key购买 nike

我需要使用正文中的 json 对象发出 POST 请求。请求如下:

request({
encoding: 'utf8',
url: 'myUrl',
json: true,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': ticket
},
json : JSON.stringify(eval("(" + str + ")"))
}, function (error, response, body) {
console.log(JSON.stringify(eval("(" + str + ")")))
res.status(200).send('added')
}
)

该请求未执行其应执行的操作。但是,如果我使用通过 console.log 获得的 JSON.stringify 评估通过 Postman 向同一 url 发出 POST 请求,则它可以工作。有什么想法吗?

最佳答案

为什么不只是

json: JSON.stringify(str)

忘记 eval() 函数,它不是必需的。 str 应该是一个 javascript 值,它将被转换为 JSON 字符串。

关于javascript - 带有 json 正文的 POST 请求 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37553707/

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