Is it possible to send an image file in the pre-request script of a request

Hello everyone!

To give context to my problem, I have a set of endpoints that are for posting, deleting and getting images. The post requests use the binary option in the Body section of the request.

I was wondering if it was possible to set the body to use the binary body option in the pm.sendRequest() function to send image data?

pm.sendRequest({
url: ‘Some URL’,
method: ‘POST’,
header: {
{{parameters}}
},
body: {
mode: ‘binary’, (Is this possible?)
data: {{binary data here}} (Is this possible?)
}, function(err, res) {
{{do stuff here}}
}
});

if this is possible, please let me know.

Try something like this:

pm.sendRequest({
    url: 'httpbin.org/anything',
    method: 'POST',
    body: 'data:image/png;base64,iVBORw0KGgoAAAANS[...]==', 
function(err, res) {
    console.log(res);
}
});

@vdespa

Thank you, that clears that part up. I have a follow up question, I was wondering if it was possible to retrieve file data from your local machine and convert it to base64, or capture the binary data from a GET request to an image URL and convert it to base64 in order to POST it on a request?

I guess not, as you cannot read files when using scripts. Of course, this could be possible in Newman with some custom scripting.

You can’t access your local file system directly for security reasons. However, as a workaround, you could set up a local server to listen for requests sent from Postman, and then access your local file system in that way.

Here’s an example of how to write to your file system - reading from your file system would be a similar process: