带nodejs的模拟卷曲请求

时间:2017-09-06 10:42:15

标签: node.js curl request

我应该向远程服务器(nodejs)发送curl post请求:

curl -X POST \
  --header 'Content-Type: application/json; charset=utf-8' \
  --header 'Accept: application/json' \
  -d '{"email":"voluumdemo@voluum.com","password":"1qaz!QAZ"}' \
  'https://api.voluum.com/auth/session'

我尝试这样的事情:

var request = require('request');
var headers = {
    'Content-Type': 'application/json; charset=utf-8',
    'Accept': 'application/json'
};

var options = {
    url: 'https://api.voluum.com/auth/session',
    method: 'POST',
    headers: headers,
    data: {"email":"voluumdemo@voluum.com","password":"1qaz!QAZ"}
};

request(options, function (error, response, body) {
  console.log('Res:'+response.statusCode);
  console.log('err:'+error);
    if (!error && response.statusCode == 200) {
        // Print out the response body
        console.log(body);
    }
});

但它不起作用。我只有400回应。

0 个答案:

没有答案