Sending Binary data via a script

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.

Hi @danny-dainton, is this an area you are familiar with? Only it’s got me foxed :slight_smile:

Not sure this is possible.

raw: Buffer.from(pm.collectionVariables.get('b64Image'), 'base64').toString()

Base64 is not binary. You would need to convert from Base64 to binary.

Postman has a couple of libraries that do that.

Postman supports btoa (binary to ASCII) and atob (ASCII to binary).

Postman JavaScript reference | Using External Libraries)

atob - npm (npmjs.com)

But this only supports text. As you have an image file, I don’t think you can use these libraries, as I don’t think Postman will store the compiled binary in memory.

This sounds like one for someone from Postman to answer (or perhaps a new feature request).

Thanks for responding, @michaelderekjones. I’ll try the atob library and see if it makes a difference, but I know Postman has deprecated it.

You are probably right, Mike. I’ve not uploaded images like this before; it’s all new to me. I could be working outside Postman’s capabilities.