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