【问题标题】:POSTing with JSON using npm request使用 npm 请求通过 JSON 发布
【发布时间】:2015-09-03 11:15:29
【问题描述】:

如何使用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


    【解决方案1】:
    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);
      }
    );
    

    请试试这个代码。

    【讨论】:

    • 这不会产生 JSON 正文
    【解决方案2】:
    var url = 'http://xxxxx'
    request({
      url : url,
      method :"POST",
      headers : {
        "content-type": "application/json",
      },
      body: {
        'id':1,
        'name':'xxxx'
      },
      json: true
    },
    

    它正在工作

    【讨论】:

    • 请添加一些解释以显示您的答案如何解决问题,并避免发布仅代码答案。
    • 假设 OP 想要 JSON 正文,这是正确的答案
    • 谢谢,看来这个是正确的。
    【解决方案3】:

    Pretty 确定 demo 相当于: https://todoist.com/oauth/access_token?client_id=0123456789abcdef&client_secret=secret&code=abcdef&redirect_uri=...

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

    【讨论】:

      猜你喜欢
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 2023-04-10
      • 2014-03-19
      • 2019-01-12
      • 2017-11-21
      相关资源
      最近更新 更多