How to store Body data in post request through jmeter

1.1k views Asked by At

I am new to jmeter, I am using jmeter to test e-commerce website. I have manage to script one scenario, which is to add a product in basket and test the response time. Now, I have observed that when i click Add button on UI, their are two requests which are getting POSTED.

for eg: stocks are updated.

As of now, I have copied the BODY from stock and pasted in jmeter sampler, but in future i may change the Sales order and update scenario, hence i want to store this Body data(Stock request which is updated) of this request dynamically, as it will change corresponding to sales order number im providing.

The problem is I am not able to store the BODY data dynamically(Only if i change the sales order here).

I know i can use pre processor in this matter, but could anyone help me with the code to get the BODY data from the request and store dynamically before sending the sample.

Basically I need a solution where I am just updating my sales order number and rest of the things will be taken care dynamically, in my case the POSTING of Body data for updating the STOCK.

Thank you in advance!

2

There are 2 answers

0
Abhishek Puri On

See basically, you're talking about Correlation. As I can understand from your concern. You need the data for the product added to the cart. Which needs data from prev request. This can be easily managed by generating two requests. Extracting from first and using that info for the second one. This will not involve any hardcoding and will work for you in an efficient manner.

1
Dmitri T On

Something like:

vars.put('bodyData', sampler.getQueryString())

should do the trick for you, if you put this code into a JSR223 PreProcessor and add this PreProcessor as a child of the HTTP Request sampler which body you need to store - you will be able to access the request body as ${bodyData} later on where required

In the above example:

More information: Top 8 JMeter Java Classes You Should Be Using with Groovy