NodeJS发布请求正文未定义

时间:2017-03-23 17:18:03

标签: javascript node.js post request

我正在向API发送一个简单的帖子请求,该API会检索其正文中的401和挑战标记。

如果我这样做:

curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"username" : "dummydata"}' "<BASER_URL>/authentication"

我可以获得所需的令牌,为php和python尝试相同,并在请求正文中获得一个令牌,但是我无法找到使用nodejs,尝试axios,request&amp; unirest并得到相同的结果。

我做错了什么?

var request = require('request');


var options = { method: 'POST',
  url: '<BASE_URL>/authentication',
  headers:
   { 'cache-control': 'no-cache',
     'content-type': 'application/json'
    },
  body: {username: "dummydata"},
  json: true };

request(options, function (error, response, body) {
  console.log(body)
});

修改

console.log(error)

error: null

console.log(response)

response:{
    "statusCode": 401,
    "headers": {
        "set-cookie": [
            "X-HSC-PREFIX=d063b4009759",
            "JSESSIONID=08C30E33724CB79C85491B4494F0068B; Path=/HsAPI/; HttpOnly"
        ],
        "transfer-encoding": "chunked",
        "server": "Apache-Coyote/1.1",
        "connection": "close",
        "date": "Thu, 23 Mar 2017 17:23:12 GMT",
        "content-type": "application/json"
    },
    "request": {
        "uri": {
            "protocol": "https:",
            "slashes": true,
            "auth": null,
            "host": "",
            "port": 443,
            "hostname": "",
            "hash": null,
            "search": null,
            "query": null,
            "pathname": "",
            "path": "/",
            "href": ""
        },
        "method": "POST",
        "headers": {
            "cache-control": "no-cache",
            "content-type": "application/json",
            "accept": "application/json",
            "content-length": 42
        }
    }
}

0 个答案:

没有答案