带参数的NodeJS PUT请求
问题描述:
我试图让使用请求URL的PUT请求:带参数的NodeJS PUT请求
request({
uri: 'http://apiurl.url/1.0/data?token=' + APItoken,
method: 'PUT',
data: [{
'content-type': 'application/json',
body: JSON.stringify(APIpostObj)
}],
json: true
},
function(error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
console.log('Server responded with:', body);
})
我得到的错误:
'Error number': 303, Error: 'Empty PUT on /data endpoint'
有需要两个参数:ID(一个数字)和bdata(JSON)。 APIpostObj将包含它们作为{“id”:33,“bdata”:{...}}。
我错过了什么?
答
你可以试试这个
request({
uri: 'http://apiurl.url/1.0/data?token=' + APItoken,
method: 'PUT',
json: [{
'content-type': 'application/json',
body: JSON.stringify(APIpostObj)
}]
},
function(error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
console.log('Server responded with:', body);
})
答
你也可以试试这个。通常对我很好。
request({
uri: url,
method: "PUT",
headers: {
'Content-type': 'application/json'
},
body: APIpostObj,
json: true
}, (error, response, body) => {
// Do Stuff
})