Raw json body how to add variable?

variables

#1

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);

#2

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.


#3

Thank you for your reply @sivcan, 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.


#4

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