# Variables in Post body external file

**URL:** <https://community.postman.com/t/variables-in-post-body-external-file/48892>\
**Category:** Help Hub\
**Tags:** variables, json\
**Created:** [June 23, 2023, 6:12pm UTC](https://community.postman.com/t/variables-in-post-body-external-file/48892 "2023-06-23T18:12:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wellyntong](https://avatars.discourse-cdn.com/v4/letter/w/258eb7/32.png) [@wellyntong](https://community.postman.com/u/wellyntong)\
**Post date:** [June 23, 2023, 6:12pm UTC](https://community.postman.com/t/variables-in-post-body-external-file/48892/1 "2023-06-23T18:12:03Z")

</div>

I have a JSON file that makes reference to an environment variable:“`packid`”, however, I keep getting the error saying that the request is invalid.

If I replace the variable with a fixed value, the process completes with no problem.  
What is going on?

**Here is the content of the JSON file:**

```auto
{
    "custrecord_hj_packagecontents_sublist":{
        "id" : "10488153"
    },
	"custrecordhj_tc_pkgcontents_lineitemitem":"60087",
	"custrecordhj_tc_pkgcontents_lineitemitem_display":"0437411P",
	"custrecordhj_tc_pkgcontents_lineitemqty":13,
	"custrecordhj_tc_pkgcontents_lineitemqtyd":13,
	"custrecordhj_tc_pkgcontents_lineitemwt":663,
	"custrecordhj_tc_pkgcont_grandparent":10488153,
	"custrecordhj_tc_pkgcont_lineitemparent":"{{packid}}",
	"custrecordhj_tc_pkgcontentslineitemdesc":"CTR; INT'L / NAVISTAR"
}

```

**Here is the error, I’m getting:**

```auto
{
    "type": "https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1",
    "title": "Bad Request",
    "status": 400,
    "o:errorDetails": [
        {
            "detail": "Error while accessing a resource. You have entered an Invalid Field Value {{packid}} for the following field: custrecordhj_tc_pkgcont_lineitemparent.",
            "o:errorCode": "USER_ERROR"
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 26, 2023, 9:19am UTC](https://community.postman.com/t/variables-in-post-body-external-file/48892/2 "2023-06-26T09:19:06Z")

</div>

How are you consuming the JSON? In the body or in a script?

At first glance, you have quotes around the variable which means it might be treated as a string.

---

<div class="post-metadata">

**Author:** ![wellyntong](https://avatars.discourse-cdn.com/v4/letter/w/258eb7/32.png) [@wellyntong](https://community.postman.com/u/wellyntong)\
**Post date:** [June 26, 2023, 1:46pm UTC](https://community.postman.com/t/variables-in-post-body-external-file/48892/4 "2023-06-26T13:46:32Z")

</div>

Thank you for your reply, mdjones…  
Here is a picture:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/d/cd2128843f3ad862a26793ff3e0c84842c7e935e.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 26, 2023, 2:41pm UTC](https://community.postman.com/t/variables-in-post-body-external-file/48892/5 "2023-06-26T14:41:06Z")

</div>

I think the issue is related to it being binary.

I copied your JSON and sent it using Postman Echo as RAW\JSON and it went through ok.

It needs the quotes otherwise it doesn’t echo back the JSON correctly.

However, when being read as a binary file, its not reading it as a variable, but treating it as a string. (The same issue as you are having).

I thought you might be able to update the body using the pre-request script, but it doesn’t look like you can update the binary data like you can for a RAW\JSON body.

```auto
console.log(pm.request.body.file);

```

All this does is return the file name.

For RAW\JSON, you can do something like this.

```auto
const body = JSON.parse(pm.request.body.raw); // retrieve current body
// update the elements\object
pm.request.body.raw = body; // write back updated body

```

I don’t have an answer for this, but can confirm using Postman Echo that it doesn’t read the variable when its posted as binary data.

---

<div class="post-metadata">

**Author:** ![wellyntong](https://avatars.discourse-cdn.com/v4/letter/w/258eb7/32.png) [@wellyntong](https://community.postman.com/u/wellyntong)\
**Post date:** [June 26, 2023, 5:33pm UTC](https://community.postman.com/t/variables-in-post-body-external-file/48892/6 "2023-06-26T17:33:15Z")

</div>

Ok, mdjones, Thank you for trying, anyway.  
The content of the JSON file is going to be variable, but I take care of everything else outside postman. I only need his field to translate properly. So, the raw/json is no an option for me. I’ve tried using the “form-data” option, and specifying a “File” type variable, however, I’ve got another error, saying: “The provided content type is not supported. Use one of the supported values: application/vnd.oracle.resource+json; type=singular; charset=UTF-8, application/vnd.oracle.resource+json; type=collection; charset=UTF-8, application/json; charset=UTF-8.”

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 27, 2023, 1:17pm UTC](https://community.postman.com/t/variables-in-post-body-external-file/48892/7 "2023-06-27T13:17:18Z")

</div>

The Content-Type header needs to match the type of data you are sending.

For form data, application/json will be supported and Postman usually automatically sets the Content-Type header for this.

You will need to confirm that the API actually supports form data before going down this road.

It does say it supports “application/json” so have you tried raw\json instead of sending it as binary?

Not sure what you mean by “the content of the JSON file is going to be a variable”.

---

<div class="post-metadata">

**Author:** ![wellyntong](https://avatars.discourse-cdn.com/v4/letter/w/258eb7/32.png) [@wellyntong](https://community.postman.com/u/wellyntong)\
**Post date:** [June 27, 2023, 1:54pm UTC](https://community.postman.com/t/variables-in-post-body-external-file/48892/8 "2023-06-27T13:54:49Z")

</div>

Well… How can I try raw/json, if I need to get the JSON content from a file in my working directory?  
Maybe that’s what’s happening here… I need to learn how to do that.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 27, 2023, 2:11pm UTC](https://community.postman.com/t/variables-in-post-body-external-file/48892/9 "2023-06-27T14:11:07Z")

</div>

[Looping through a Data File in the Postman Collection Runner | Postman Blog](https://blog.postman.com/looping-through-a-data-file-in-the-postman-collection-runner/)

---

<div class="post-metadata">

**Author:** ![wellyntong](https://avatars.discourse-cdn.com/v4/letter/w/258eb7/32.png) [@wellyntong](https://community.postman.com/u/wellyntong)\
**Post date:** [July 7, 2023, 3:36pm UTC](https://community.postman.com/t/variables-in-post-body-external-file/48892/10 "2023-07-07T15:36:56Z")

</div>

When I say that “The content of the JSON file is going to be variable”, I mean exactly that. The JSON file is going to be different on each run, as simple as that.  
Now, I’ve just decided to work around this issue, by not using variables in my JSON file, and just do a third request to do an update of the record I’m creating, with the value in an environment variable. This worked for my need right now.  
I guess this case can be closed without a real resolution as of why the system could not replace the variable in the external JSON file I am using for the request body, with the value of the environment variable.  
Thank you for trying, thou.
