节点 js post 内容类型表单数据得到空响应
Node js post content type form-data getting null response
我正在尝试 post 使用节点 js 请求形成数据内容类型,但每次我收到响应时 'null',我都可以这样做并使用post 伙计,我真的不知道代码有什么问题,这是我的来源
var req = require('request');
req.post({
url: 'url’,
form: {request:[{"request":"context","sequence":0,"userToken":"mytokenxyz"}]},
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
'Content-Type' : 'application/x-www-form-urlencoded'
},
method: 'POST'
},
function (e, r, body) {
console.log(body); //here the body I am getting null
});
这是我得到正确响应的 postman 屏幕截图。我尝试了多种其他方法,但最终得到了相同的空响应。
it is working for me try like that
var request = require("request");
var options = { method: 'POST',
url: 'http://localhost:3000/admin/assignprivilege',
headers:
{ 'Postman-Token': 'b93555a6-46de-04f9-af2d-69ebe38ea3dd',
'Cache-Control': 'no-cache',
'Content-Type': 'application/json' },
body:
{ privilege:
[ { role_id: 1,
mod_id: 1,
p_access: 'ON',
p_insert: 'ON',
p_updete: 'ON',
p_delete: 'ON',
created_by: 1 },
{ role_id: 1,
mod_id: 1,
p_access: 'ON',
p_insert: 'ON',
p_updete: 'ON',
p_delete: 'ON',
created_by: 1 },
{ role_id: 1,
mod_id: 1,
p_access: 'ON',
p_insert: 'ON',
p_updete: 'ON',
p_delete: 'ON',
created_by: 1 },
{ role_id: 1,
mod_id: 1,
p_access: 'ON',
p_insert: 'ON',
p_updete: 'ON',
p_delete: 'ON',
created_by: 1 } ] },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
Also try like that it can work
"privilege":[
{
"role_id":1,
"mod_id":1,
"p_access":"ON",
"p_insert":"ON",
"p_updete":"ON",
"p_delete":"ON",
"created_by":1
},
{
"role_id":1,
"mod_id":1,
"p_access":"ON",
"p_insert":"ON",
"p_updete":"ON",
"p_delete":"ON",
"created_by":1
},
{
"role_id":1,
"mod_id":1,
"p_access":"ON",
"p_insert":"ON",
"p_updete":"ON",
"p_delete":"ON",
"created_by":1
},
{
"role_id":1,
"mod_id":1,
"p_access":"ON",
"p_insert":"ON",
"p_updete":"ON",
"p_delete":"ON",
"created_by":1
}]
var obj = data['privilege'];
for (var key in obj) {
console.log(obj[key].role_id);
}
问题是由于 mytokenxyz 中的反斜杠没有正确转义
form: {request:[{"request":"context","sequence":0,"userToken":"mytokenxyz"}]},
实际令牌是 B4nx4tt4m!\\MK_API
我正在尝试 post 使用节点 js 请求形成数据内容类型,但每次我收到响应时 'null',我都可以这样做并使用post 伙计,我真的不知道代码有什么问题,这是我的来源
var req = require('request');
req.post({
url: 'url’,
form: {request:[{"request":"context","sequence":0,"userToken":"mytokenxyz"}]},
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
'Content-Type' : 'application/x-www-form-urlencoded'
},
method: 'POST'
},
function (e, r, body) {
console.log(body); //here the body I am getting null
});
这是我得到正确响应的 postman 屏幕截图。我尝试了多种其他方法,但最终得到了相同的空响应。
it is working for me try like that
var request = require("request");
var options = { method: 'POST',
url: 'http://localhost:3000/admin/assignprivilege',
headers:
{ 'Postman-Token': 'b93555a6-46de-04f9-af2d-69ebe38ea3dd',
'Cache-Control': 'no-cache',
'Content-Type': 'application/json' },
body:
{ privilege:
[ { role_id: 1,
mod_id: 1,
p_access: 'ON',
p_insert: 'ON',
p_updete: 'ON',
p_delete: 'ON',
created_by: 1 },
{ role_id: 1,
mod_id: 1,
p_access: 'ON',
p_insert: 'ON',
p_updete: 'ON',
p_delete: 'ON',
created_by: 1 },
{ role_id: 1,
mod_id: 1,
p_access: 'ON',
p_insert: 'ON',
p_updete: 'ON',
p_delete: 'ON',
created_by: 1 },
{ role_id: 1,
mod_id: 1,
p_access: 'ON',
p_insert: 'ON',
p_updete: 'ON',
p_delete: 'ON',
created_by: 1 } ] },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
Also try like that it can work
"privilege":[
{
"role_id":1,
"mod_id":1,
"p_access":"ON",
"p_insert":"ON",
"p_updete":"ON",
"p_delete":"ON",
"created_by":1
},
{
"role_id":1,
"mod_id":1,
"p_access":"ON",
"p_insert":"ON",
"p_updete":"ON",
"p_delete":"ON",
"created_by":1
},
{
"role_id":1,
"mod_id":1,
"p_access":"ON",
"p_insert":"ON",
"p_updete":"ON",
"p_delete":"ON",
"created_by":1
},
{
"role_id":1,
"mod_id":1,
"p_access":"ON",
"p_insert":"ON",
"p_updete":"ON",
"p_delete":"ON",
"created_by":1
}]
var obj = data['privilege'];
for (var key in obj) {
console.log(obj[key].role_id);
}
问题是由于 mytokenxyz 中的反斜杠没有正确转义
form: {request:[{"request":"context","sequence":0,"userToken":"mytokenxyz"}]},
实际令牌是 B4nx4tt4m!\\MK_API