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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With