0

I am trying to post form-data content type using node js request, but every time I am getting a response 'null', I can do the same and get the correct response using the post man, I really don’t know what is wrong in the code, here is my source

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

Here is the postman screenshot where I am getting the correct response. I tried a number of other ways but ends up in the same null response. enter image description here

1
  • The first thing I notice is that in Postman you have the MIME for the request as application/form-data, but in your code you are using application/x-www-form-urlencoded. Commented Dec 23, 2017 at 23:49

3 Answers 3

1

The issue was due to the Backslash in the mytokenxyz that wasn't escaped properly

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

the actual token was B4nx4tt4m!\\MK_API

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.