The Mechanic
The Mechanic

Reputation: 2329

Not getting proper response in node js by using node-rest-client middleware

I am a newby in Node js, after hit HubSpot api through node js by using node-rest-middleware am getting below kind of response. Why?

<Buffer 7b 22 76 69 64 22 3a 35 2c 22 63 61 6e 6f 6e 69 63 61 6c 2d 76 69 64 22 3a 35 2c 22 6d 65 72 67 65 64 2d 76 69 64 73 22 3a 5b 5d 2c 22 70 6f 72 74 61 ... >

Here is my snippet please take a look

var args = {
    data: formData,
    headers:{"Content-Type": "application/json"}
};

client.post(hubURL, args, function(data,response) {
      // parsed response body as js object 
      console.log(data, "data in client post");
});

and formData is a proper valid json what am doing wrong

updated here is the snapshot enter image description here

Upvotes: 4

Views: 5108

Answers (3)

Mounir Babari
Mounir Babari

Reputation: 80

The Buffer should be converted to a String

client.post(baseUri + "tradingpartners/",args, function (data, response) {
    // parsed response body as js object
  console.log(data.toString('utf8'));
});

Upvotes: 0

Mritunjay
Mritunjay

Reputation: 25882

The data you are getting is a Buffer. Try converting it to string like bellow.

if(Buffer.isBuffer(data)){
    data = data.toString('utf8');
}
console.log(data);

Upvotes: 4

Gaurav Gupta
Gaurav Gupta

Reputation: 4691

This is data formatting issue, you are getting data in Buffer format and you want it in String format.

You can explicitly do the conversion.

You can use toString() of the Buffer, which will convert Buffer format into String format. Refer

var yourStringData = data.toString('utf8');

Upvotes: 1

Related Questions