GO VEGAN
GO VEGAN

Reputation: 1143

post request - node js (tests) - java script

I write a test which firstly has to do post request. I get an error when I run this :

the url is : http://walla.com:8080/internal/getToken

the code :

function user(){
    var postData = {
        loginName: '[email protected]',
        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();
    });
});

Am i doing something wrong? thanks?

Upvotes: 1

Views: 161

Answers (1)

thefourtheye
thefourtheye

Reputation: 239573

You are missing a comma after the 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'}
};

Instead, you can directly define the object, like this

var options = {
    host: 'http://walla.com',
    port: '8080',
    path: '/internal/getToken',
    body: {
        loginName: '[email protected]',
        password: 'abcdef'
    },
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    }
};

Upvotes: 1

Related Questions