JSON request body with html content (from local file)

Hi,
I just started working with postman and wanted to see if I could get some help with constructing the POST request body.

My sample request body (JSON) is

{
“id”: “correlation_id”
“data”: “HTML_STRING”
}

As I understand we’d need to escape the special characters in HTML_STRING. What would be the best way to achieve loading the html content from file and having it as a part of the request as shown above, in Postman? Do I need to get the escaped content with some external script/online tool first before I formulate the request and then POST to my API ?

Hello @4rk, Welcome to the community :tada:

There are two options:

  1. If you are writing an external script for creating a file, I would suggest escaping the special characters and then putting the HTML content in the file.
  2. you can use postman’s pre-request script for escaping the special characters in HTML content.

To use the HTML content as a part of API request:

  1. Create a data file with id and data
  2. Iterate the file and access the value from the file using pm.iterationData.get("data")
  3. Set the environment/global/collection variable after getting data from file.
  4. Use Dynamic variable for assign value within JSON body.

I hope this works for you.

Cheers :magic_wand:

Here the data, unless is escaped in the json/csv, does not seem valid. Doesn’t that necessitate that extra step to escape the data content first? Also, another example of the content I might send, on a different endpoint, is an email (headers with plain/html content), so not exactly just HTML. My apologies, if that would change the context and content of your original answer. I thought they could be handled in the same fashion.

1 Like