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

1 Like