Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用npm请求使用JSON发布_Node.js_Oauth_Request - Fatal编程技术网

Node.js 使用npm请求使用JSON发布

Node.js 使用npm请求使用JSON发布,node.js,oauth,request,Node.js,Oauth,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: '0123456

如何使用
请求
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) {
  ...
});
非常确定该演示相当于:
https://todoist.com/oauth/access_token?client_id=0123456789abcdef&client_secret=secret&code=abcdef&redirect_uri=...

其中,
启动参数,而
将它们分开

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);
  }
);
请尝试此代码

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

它起作用了

请添加一些解释,说明您的答案是如何解决问题的,并避免发布纯代码的答案。假设OP想要一个JSON正文,这是正确的答案。这不会导致JSON正文