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?
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:
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:
Now, I want to know how can I specify a binary data in body tag? Is that even possible using pre request script?
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.
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.
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.