post request - node js(tests) - java脚本

时间:2014-04-29 05:40:38

标签: javascript node.js http jasmine

我写了一个首先要做帖子请求的测试。我运行时遇到错误:

网址是:     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();
    });
});

我做错了吗? 感谢?

1 个答案:

答案 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'
    }
};