Get body variables from Pre-Request Script

Hi there,

I have a request to a specific End Point with several parameters on its body (form-data) with POST method as follow:

login: {{int value}}
name: {{string value}}
amount: {{int value}}
mode: DEPOSIT/WITHDRAW
hash: *

  • this hash value, is a simple verification variable on the endpoint side to verify that this request is coming from a legitimate client.

Hash should contain MD5 value from these following variables (in sequence: MD5(login + amount + date + balance).

So the endpoint side can compare the request from the client with the hash value parameters if match with above MD5 formula means that the request came from a legitimate client. Otherwise, the endpoint will throw an error …

Question is:

I made a Pre-Request Script with a following code:

var balance = pm.variables.get(“balance”);
var login = pm.variables.get(“login”);
var amount = pm.variables.get(“amount”);
var date = pm.variables.get(“date”);
var hash = CryptoJS.MD5(login + amount + date + balance).toString();
pm.environment.set(“hash”, hash);

This script is meant to set the hash value on the Body (form-data) variable to be set automatically with the MD5 value of (login + amount + date + balance).

Problem: The above code didn’t work. None of the above variables can read the value from what I’ve put on the body variables (all reads null on the console) although I already use pm.variables.get({{variable-name}});

Question:

#1. How can I get variables values from Body (form-data) variable? Not variable from environment / global.
#2. How to put/set the variable value on Body (form-data) before sending the request to the endpoint?



Would appreciate! if someone can help me with this issue.

Thanks.

Regards,

1 Like

Hi @RUSDI,
Welcome to the community!

There are a couple of things I want to mention before we can talk about the solution

  1. In the screenshots you provided, you seem to be trying to fetch the body parameters through pm.variables.get which will not work. Please refer to https://learning.getpostman.com/docs/postman/environments_and_globals/variables/ for additional information about how Variables work in Postman
  2. You can however set values in your environment/variables in Pre-request script and use the same variables in your body

You can refer https://learning.getpostman.com/docs/postman/environments_and_globals/intro_to_environments_and_globals/#___gatsby to learn more about Environments in Postman

The solution in this case would be to set your balance, login, amount and date in your environment and set the hash in your Pre request script. You can fetch the same variables in your body.

Also, mutating request body in pre-request script is something that is being tracked here → https://github.com/postmanlabs/postman-app-support/issues/4808

1 Like

Hi there,

Thank you for your reply. So at this moment, what is the most useful to do with Pre-Request script if can’t modify the body variable -or- even can’t get the variable value from the body value?

Hi @RUSDI,
Right now, like you mentioned, you can’t modify the body variable from the pre-request script. However, assuming you use the environment/variable /globals approach in the pre-request script, you can technically still set these in the body after assigning values in the pre-request script.

Also, the concept of pre-request script becomes even more powerful when you look at running a collection. I will try to list down use cases of pre-request scripts that I can think of -

  1. Mutating headers from the script (https://github.com/postmanlabs/postman-app-support/issues/4631#issuecomment-477766260)
  2. Setting/unsetting cookies (https://github.com/postmanlabs/postman-app-support/issues/3312#issuecomment-516965288)
  3. Make a request to a secondary URL and use the response for your request

But, as per your use-case, mutating request body in pre-request script is being tracked and is on the roadmap

2 Likes

How can we read the request body / request query / form url encoded values from the post request/test script?

@sreekanthbhargav Warm welcome to the Community :slightly_smiling_face:

You need to store the responseBody in an object and it can used further. For example:

var jsonData = JSON.parse(responseBody);

So here I have stored the entire body, here JSON.parse() method, converts the response into the JavaScript object. Ans then you can access the body using the variable “jsonData”.

Based on your need you can store the response.

If this doesn’t answer your question, revert back with ore details :slightly_smiling_face:

Using JSON.parse(responseBody) is the old sandbox syntax - I would advice using pm.response.json() going forward.

All the other pm.* methods can be found here:

2 Likes

The cleanest way to interrogate the body now seems to be:

data = pm.request.body;
body = data[data.mode];
jsonData = JSON.parse(body);

‘pm.request.body’ returns an object containing the mode together with a member named after that mode. You need to get the value of that member, to have the actual contents you specified for the body. Since that is likely a string, you then need to parse it.

1 Like