[英]POST request with a json body javascript
I need to make a POST request with a json object in the body. 我需要在主体中使用JSON对象发出POST请求。 The request is the following:
该请求如下:
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')
}
)
The request doesn't do what it's supposed to do. 该请求没有执行应做的事情。 However, if I use the JSON.stringify evaluation that i get with console.log to make a POST request to the same url with Postman, it works.
但是,如果我使用console.log附带的JSON.stringify评估来对Postman发出与同一URL的POST请求,则它可以工作。 Any ideas why?
有什么想法吗?
Why not just 为什么不只是
json: JSON.stringify(str)
Forget the eval() function, it's not needed. 忘记eval()函数,不需要它。 str should be a javascript value, which will be converted to a JSON string.
str应该是一个javascript值,它将转换为JSON字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.