Raw json body how to add variable?

Hello,

How can I add variable to raw - json body?

 {
    "referenceId": "022078925508",
    "productCode": "001002461285",
    "quantity": "1",
    "version": "V1",
    "signature": {{Signature}},
    "applicationCode": "52e7cf966b724749a7c4efadc3727ed7"
}

Here is my pre-request script

var ApplicationCode = request.data["ApplicationCode"];
var Version=request.data["version"];
var ReferenceId=request.data["referenceId"];
var ProductCode=request.data["productCode"];
var Quantity=request.data["quantity"];
var SecretKey="e56067172e61";
var hashText = ApplicationCode+ProductCode+Quantity+ReferenceId+Version+SecretKey;
var encryption = CryptoJS.MD5(hashText).toString();

postman.setEnvironmentVariable("Signature", encryption);

Hi @kizildagcenk, you’re using the correct method to set the variable in the pre-request script.
One thing I’d like to point out on the last line is that you can use pm.environment.set("Signature", encryption);
This is the new API and I recommend using it.

Also, in the raw body, you’ve to surround the Signature variable with quotes.
So your raw body actually becomes the following:

{
    "referenceId": "022078925508",
    "productCode": "001002461285",
    "quantity": "1",
    "version": "V1",
    "signature": "{{Signature}}", // notice the quotes here around the variable
    "applicationCode": "52e7cf966b724749a7c4efadc3727ed7"
}

Now run your script and it should work as expected.

Thank you for your reply @singhsivcan, I m getting error from the service. I think they want content-type = application/x-www-form-urlencoded. Is there a way to convert this to application/x-www-form-urlencoded?

Best Regards.

@kizildagcenk
You can try this:
Disable the first header.
And then you can add the other header like so:

Or if this fits your case then you can use the body type x-www-form-urlencoded instead of raw
This will automatically add the header too.

I know this is an older post/has been closed for a while. But is there a limit to the variable size? I am trying to get a description from a JIRA issue and use it as a variable to create another one.

The syntax you mention, “{{Variable}}”, works for me in Body Raw JSON when using variables such as Date or Title from the JIRA issue, but when using the description I am getting the following error:

{
“errorMessages”: [
“Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value\n at [Source: org.apache.catalina.connector.CoyoteInputStream@7fc7314c; line: 13, column: 39]”
]
}

I am able to get the request to work when I copy in the description value into the JSON Body (even its entire length )

Any suggestions?

Edit:

So I was able to answer my own question.
Using stringify() to set my variable seems to fix my problem:

pm.environment.set(“description”, JSON.stringify(description));

Thank you, @davidbeinhart. It works for me.

Since this question is still pulling a lot of views we’ve created a collection that shows how to achieve that in the Postman Answers public workspace:

https://www.postman.com/postman/workspace/postman-answers/collection/9215231-f6b56d11-9a88-4785-8b78-ebd756b6c940?ctx=documentation

Fork it and see how it works by yourself!

Hi! I am in a similar situation, but this didn’t completely work for me. I have my request in a folder that contains a pre-request script to calculate an HMAC over the request’s body.

What seems to happen is that the pre-request script of the request is run after the pre-request of the folder, so I tried moving the

pm.collectionVariables.set('shopName', pm.environment.get('shopName'));

to the first line of the folder’s pre-request script but that still didn’t work.

This is what the pre-request script of the folder looks like:

const input = request.data;
const key = pm.environment.get("key");
const hmac = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(input, key));
pm.environment.set("hmac",hmac)

Like I said, using the collectionVariables.set at the beginning of the script didn’t work. What worked was changing the first line to:

const input = request.data.replace("{{shopName}}",pm.environment.get("shopName"));

What is the best approach in a situation like this?

You just made my day! Not in a million years would I imagine that the missing quotes were causing the empty array problem. Thanks!

you can find short demo explanation here how to add variable in json postman

It works, but does’t show the value of the variable on mouse over. Notice that the var name appears in italic.
2022-12-27_11-03

I’m in a similar situation, but it’s a little different.

{
    "items": [
   {
    "id":"123456" 
   },           
     {
    "id":"1234567" 
   }
]  
 }

What I want to achieve is to generate the items array elements, because I have to send 10.000 items in one POST request.
How can I replace the array’s items?

Hey @scheiblib94 :wave:

Welcome to the Postman Community! :postman:

You would need to do this:

{
    "items": {{array}}
}

You would need to add this to the pre-request to construct the request body:

let reqArray = [
   {
    "id":"123456" 
   },           
     {
    "id":"1234567" 
   }
]

pm.variables.set('array', JSON.stringify(reqArray));

It will be slightly different if you’re pulling that data from a variable but be you’re need to ensure that it’s in the correct format of it wouldn’t resolve the data correctly.