How to send a POST request with binary data as request body?

Hi,

I can send a POST request using binary data as a response body successfully using the normal POST request but I wanted to replicate the same behavior using the pre-request script.

The only thing I am struggling with is the creation of the request body. For example, let’s say I have a jpg file that needs to be converted to binary data and then it to be added the request body.

Also, I noticed the library ‘fs’ which is used to convert file to binary data is giving an error “fs.readFileSync is not a funtion”. Is there a workaround?

Thanks!!

Hi @rb12121

This post may help answer your question about fs.readFileSync

Is that jpeg confidential?

Are you ok with sending the jpeg to the Postman Echo service?

If you send the request as form data, it will return the base64 encoding.

For example…

image

image

You can parse the response using something similar to the following to just return the base64 encoded string,

let response = pm.response.json(); 

let base64 = response.files["dog.jpg"].split(",")[1];

console.log(base64)

If you want to send this is a pre-request script then it will be similar but you will need to use the pm.sendRequest() function.

1 Like

Hi @michaelderekjones ,

I mentioned the file type as .jpg just for the sake of simplicity. The file type that I am dealing here with is “dcm”.

The service that I am trying to test requires a dcm file to be converted to binary data first, then this binary data needs to be part of the POST request body. I’ll try to attach some images for reference:

image

If I send this request with the appropriate authorization and content-type header, it will be posted to the service without any problem. But I want to automate this and I want it to be the part of my pre request script. This is as far as I’ve come:

image

Now, I want to know how can I specify a binary data in body tag? Is that even possible using pre request script?

Thanks!

My bad, I thought you meant base64 encoding of a binary file.

When I send a request to Postman Echo using body\binary like in your example, the request that was sent shows the following.

image

It’s not a JSON object.

Postman Echo doesn’t seem to Echo this type of binary body, however as you’ve mentioned this works if you send it manually, then I’m assuming the request is ok.

Send your request manually and then have a look at the console log to see what was sent to the API. You should be able to see what structure was sent for the body.

Also use the code snippets on the right to see how this is sent via curl.

My example shows…

curl --location 'https://postman-echo.com/post' \
--header 'Content-Type: image/jpeg' \
--header 'Cookie: sails.sid=s%3A4OlcoFEuO3tX_O0cCEULPioomWdSE0cy.dYXZknojsVuJLC0YHwFw7MJR5TnTDQ%2FtOTiBY4U474o' \
--data '@-nhzigCYz/dog.jpg'

It doesn’t look like it’s meant to go in the body tags.

I don’t know if you can change the folder\filename to pick it up from the Postman working directory found in Settings\General (but this won’t really help if you want to pick up the file from a central location).

Which means you are back to Newman and node.js (so you can include the fs library).

Here are some links and examples if you have to go that route.

newman - npm (npmjs.com)

[Feature] possibility to programatically write to a file · Issue #3033 · postmanlabs/postman-app-support · GitHub

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.