Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the raw HTTP message body using the request library in Node.js?

The npm-request library allows me to construct HTTP requests using a nice JSON-style syntax, like this.

request.post(
    {
        url: 'https://my.own.service/api/route',
        formData: {
            firstName: 'John',
            lastName: 'Smith'
        }
    },
    (err, response, body) => {
        console.log(body)
    }
);

But for troubleshooting, I really need to see the HTTP message body of the request as it would appear on the wire. Ideally I'm looking for a raw bytes representation with a Node.js Buffer object. It seems easy to get this for the response, but not the request. I'm particularly interested in multipart/form-data.

I've looked through the documentation and GitHub issues and can't figure it out.

like image 333
Nic Avatar asked Aug 02 '19 23:08

Nic


People also ask

What is raw body in HTTP request?

Raw body content refers to the ability for you to document APIs that have no named JSON parameters. Something like the following in cURL: cURL. curl --request POST \ --url https://httpbin.org/post \ --header 'content-type: application/json' \ --data 'string' This also works for other primitives, arrays and objects.


1 Answers

Simplest way to do this is to start a netcat server on any port:

$ nc -l -p 8080

and change the URL to localhost in your code:

https://localhost:8080/v1beta1/text:synthesize?key=API_KEY

Now, any requests made will print the entire, raw HTTP message sent to the localhost server.

Obviously, you won't be able to see the response, but the entire raw request data will be available for you to inspect in the terminal you have netcat running

like image 99
Swayam Avatar answered Oct 31 '22 19:10

Swayam