使用npm请求使用JSON进行POST

问题描述 投票:8回答:2

如何使用request npm模块执行以下操作?

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

我试过这样做:

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) {
  ...
});
node.js oauth request
2个回答
22
投票
const 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);
  }
);

请试试这段代码。


0
投票

非常确定该演示相当于:https://todoist.com/oauth/access_token?client_id=0123456789abcdef&client_secret=secret&code=abcdef&redirect_uri=...

?开始参数,&将它们分开。

© www.soinside.com 2019 - 2024. All rights reserved.