Type Error "string" must be a string, Buffer, or ArrayBuffer

Viewed 4997

I am in Node trying to send a http call to an endpoint. I'm not getting any response, and im certain I have set something up incorrectly. My request worked on postman. Any help woud be awesome!

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

curlUpdate = function curlUpdate() {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "www.example.com", false);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send({
        FirstName: "john",
        LastName: "doe",
        MerchantSessionID: "817281271",
        DateOfBirth: "12,24,1985",
        ProductTypeID: "xxxxxxxxx"
    });
    console.log(xhr.responseText);
};
1 Answers

You just need to wrap your object with JSON.stringify().

So your xhr.send will looks like:

xhr.send(JSON.stringify({
        FirstName: "john",
        LastName: "doe",
        MerchantSessionID: "817281271",
        DateOfBirth: "12,24,1985",
        ProductTypeID: "xxxxxxxxx"
    }));

P.S. don't forget to point address (now it's www.example.com in your code) to correct endpoint.

Related