Tuesday 6 June 2017

npm request.post giving error everytime but the same post works with JQuery

I am trying to make a post request from express js to an api, from serverside.

Here is the code for it

request.post({
                url: $url,
                crossDomain: true,
                data: {id : '392', sid: 'abc'}
                }, function(error, response, body){
                    console.log(response.statusCode);
                    console.log(body);
            });

this always returns with a response statusCode 500;

On the other hand, I tried the same post request from server side using Jquery.

Here is the code for it.

$.ajax({
    url: $url,
    crossDomain:true,
    data: {
        id : $id,
        sid: $sid
    },
    type: "POST",
    // dataType: "json",

    //on success
    success: function(response){
        //do something after something is received data
    },
    //on error
    error: function(jqXHR, exception){
        //bad request
        console.log(jqXHR.status);
        console.log(exception);
    }
});

The $url and all the other variables are same in both the cases. Though, I get proper response and status code 200 for the post I do with Jquery, unlike the one I do with npm request module.

Where am I going wrong?



via krishna

No comments:

Post a Comment