Variables in Post body external file

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:

{
    "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:

{
    "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"
        }
    ]
}

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.

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

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.

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

All this does is return the file name.

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

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.

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.”

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”.

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.

Looping through a Data File in the Postman Collection Runner | Postman Blog

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.