【发布时间】:2019-10-11 11:13:06
【问题描述】:
我想知道节点HTTP请求中json和data的区别
var http = require('http');
let api = {
url: "https://someurl/api/ticket/create",
method: "POST",
headers:
{
"cache-control": "no-cache",
"Content-Type": "application/json",
"Accept": "application/json"
},
data:{
}
}
let apiTwo = {
url: "https://someurl/api/ticket/create",
method: "POST",
headers:
{
"cache-control": "no-cache",
"Content-Type": "application/json",
"Accept": "application/json"
},
json:{
}
}
http.request(api,function(err,resp,body){ // 400 for api and 200 for apiTwo
if(err){
console.log(err);
}
else{
console.log(resp);
console.log(body);
}
})
当我在请求中使用data 作为键访问上述API 时,我得到400。
当我使用json 作为请求中的键访问相同的API 时,我得到200。
简而言之,我的问题是这个json和data是在服务器上配置的吗?它们之间有什么区别?什么时候首选?
【问题讨论】:
-
Neither of the two is documentented 所以我很困惑为什么它们甚至会影响事物。
-
我也很困惑:)
-
你能添加需求行吗? (例如
const http = require('http');)(如果您使用一些库,我们知道。) -
已经有需求行,我没有添加有问题的
-
我已将其添加到问题中。
标签: javascript node.js json http request