节点js发布内容类型表单数据获取空响应

时间:2017-12-23 12:29:42

标签: node.js http post form-data

我正在尝试使用节点js请求发布表单数据内容类型,但每次我得到响应'null'时,我都可以这样做并使用帖子man得到正确的响应,我真的不知道代码中有什么问题,这是我的来源

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
  });

这是邮递员截图,我得到了正确的回复。我尝试了许多其他方法,但最终得到了相同的null响应。 enter image description here

3 个答案:

答案 0 :(得分:0)

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);
});

答案 1 :(得分:0)

   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);
        }

答案 2 :(得分:0)

问题是由于mytokenxyz中的反斜杠未正确转义

form: {request:[{"request":"context","sequence":0,"userToken":"mytokenxyz"}]},

实际令牌是B4nx4tt4m!\\ MK_API