how to make post call using superagent

8.8k views Asked by At

I am using following code to make an Post call using superagent

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var request = require( "superagent" );
var PostUrl = 'http://example.com/rest/api/someapi';

var body = ["true"];
var data = JSON.stringify(body);
console.log("data: " + data);

request.post( PostUrl ).send('[true]').set('Content-Type', 'application/json').auth('user', 'pass').end(function(err,res){
    if(err){
        console.log("CreateUsers post error = ", err )
    } else {
        console.log("CreateUsers post response = ", res.status)
        console.log(res.body);
    }
});

Error says that its not able to find the api. If I use postman to make same restapi call using 'POST' method; it works fine.

Also, when i looked at error object, the method mention there is 'GET' and not 'POST'.

Here is the request header that i got when i printed the response object

_header: 'GET /zebi/api/haService/getLocalNodeName HTTP/1.1\r\nHost: 10.67.1.61\r\naccept-encoding: gzip, deflate\r\nuser-agent: node-superagent/3.3.0\r\nauthorization: Basic YWRtaW46dA==\r\nConnection: close\r\n\r\n',

Can someone please help me with this.

0

There are 0 answers