POSTing with JSON using npm request

How to do the following with 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 tried to do 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) { ... }); 
+5
source share
2 answers
 var formData = { client_id: '0123456789abcdef', client_secret: 'secret', code: 'abcdef' }; request.post({ url: 'https://todoist.com/oauth/access_token', form: formData }, function (err, httpResponse, body) { console.log(err, body); }); 

Please try this code.

+11
source

Quite precisely, the demo is equivalent: https://todoist.com/oauth/access_token?client_id=0123456789abcdef&client_secret=secret&code=abcdef&redirect_uri=...

Where is a ? launches parameters, and & separates them.

0
source

All Articles