Hi There,
I hope someone can help as I’ve spent quite a bit of time trying to resolve this with no success,
I want to send a binary image to an API using the pre-request script.
Firstly I tried using the standard Postman request, setting the type to Binary and attaching an image file, which worked successfully.
With this approach, there is no way to share this with the team as the file is locally hosted on my machine. We are using an enterprise license so we can’t share files I believe.
So I created a pre-request similar to this to workaround it. I have the contents of png file base64 encoded in a collection variable called b64Image
const postPodImageRequest = {
method: 'POST',
url: 'https://qa-services.test.com/v1/images/',
header: {
'x-image-type': 'photo',
'Content-Type': 'image/png',
'Accept': '*/*',
'User-Agent': 'PostmanRuntime/7.43.0',
'Cache-Control': 'no-cache',
},
body: {
mode: 'binary',
raw: Buffer.from(pm.collectionVariables.get('b64Image'), 'base64').toString()
}
}
await pm.sendRequest(postPodImageRequest, (error, response) => {
if (error) {
console.log(error);
} else if (response.code !== 201) {
throw new Error(`incorrect status code returned, expecting 201 and received ${response.code}`);
} else {
pm.test(`Image sent to Image service`, () => {
pm.expect(response.code).to.eql(201);
});
}
});
Unfortunately, this request fails with a 400 status code, complaining about the content of the message.
I tried sending it via a burp suite proxy to compare the requests and one thing I noticed was the request via the script is being sent with a mine type of JSON, whereas the working request was sent as text, I’m not sure if that is the issue.
I tried a similar request with the playwright and it worked the first time, so I’m not sure if this is a bug in Postman?
Has anyone else been able to successfully send a binary image via a script, where the image is the body of the request? I know you can send images as form data too, but this API doesn’t support it.