我写了一个首先要做帖子请求的测试。我运行时遇到错误:
网址是: http://walla.com:8080/internal/getToken
代码:
function user(){
var postData = {
loginName: 'hello@gmail.com',
password: 'abcdef'
}
var options = {
host: 'http://walla.com',
port: '8080',
path: '/internal/getToken',
body: postData
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
var callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
}
var req = http.request(options, callback);
req.end();
}
describe("ccc", function () {
it("bbbb" , function(){
user();
});
});
我做错了吗? 感谢?
答案 0 :(得分:1)
您在body
var options = {
host: 'http://walla.com',
port: '8080',
path: '/internal/getToken',
body: postData, // Note the Comma at the end
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
相反,您可以直接定义对象,例如
var options = {
host: 'http://walla.com',
port: '8080',
path: '/internal/getToken',
body: {
loginName: 'hello@gmail.com',
password: 'abcdef'
},
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};