Set the JSON Body from an External File in Postman

Some APIs require you to send a JSON request body where one of the properties represents the contents of a file, quite often, base64 encoded. So how to do this?

Valentin Despa
APIs with Valentine

--

The problem with uploading a file when using JSON

In Postman, you can easily upload a file when creating a request with a multipart/form-data body type. But how to do the same when the request body is JSON. Let’s take this example request body:

{
"description": "My cool file",
"base64String": "SGVsbG8gd29ybGQK"
}

If we already know the base64 value of the file we want to send over, we can simply paste the value into the request body.

However, this would be inconvenient and leave very little room for automation.

Unfortunately, in Postman is not possible to specify the path to a file from JSON.

Additionally, it is not possible to read the contents of a file from a Postman script.

The approach

The only workaround is to use another API that can expose the content of the file through HTTP. Once we have the file content in an HTTP response, we will set it as a Postman variable and use it in our intended request.

Using httpbin.org as an INSECURE way of reading files

--

--

Valentin Despa
APIs with Valentine

Software developer, educator & overlander • GitLab Hero • AWS Community Builder • Postman Supernova • Imprint: http://vdespa.com/imprint