Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSTing with JSON using npm request

How would one do the following with the request npm module?

curl https://todoist.com/oauth/access_token \
    -d client_id=0123456789abcdef \
    -d client_secret=secret \
    -d code=abcdef \
    -d redirect_uri=https://example.com

I've tried doing this:

var body = JSON.stringify({ 
  client_id: '0123456789abcdef', 
  client_secret: 'secret', 
  code: 'abcdef'
});

var postBody = {
  url: 'https://todoist.com/oauth/access_token',
  body: body,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
};

request.post(postBody, function(error, response, body) {
  ...
});
like image 819
Kirby Kohlmorgen Avatar asked Jun 18 '15 00:06

Kirby Kohlmorgen


1 Answers

var url = 'http://xxxxx'
request({
  url : url,
  method :"POST",
  headers : {
    "content-type": "application/json",
  },
  body: {
    'id':1,
    'name':'xxxx'
  },
  json: true
},

it's working

like image 73
Sajeenthiran Avatar answered Sep 17 '22 17:09

Sajeenthiran