如何使用Jmeter存储整个响应并为下一次休息呼叫更新它 [英] How to store entire response and update it for next rest call using Jmeter
问题描述
问题:如何提取整个restAPI响应并使用某些值对其进行修改,以及如何将更新后的响应用于后续的rest调用.我正在使用Jmeter
Question: How to extract entire restAPI response and modify it with some values and use the updated response for sub-sequent rest call. I am using Jmeter
我有一个POST呼叫,例如:"/check1/id1/post"
POST正文:
I have one POST call ex: "/check1/id1/post"
POST body :
{
"test":"rest",
"check" :{
"id":1,
"name": "xyz"
}
}
POST呼叫响应:
{
"test":"rest",
"check" :{
"id":1,
"name": "xyz"
"status":"updated"
}
}
================================================ =====================
=====================================================================
问题:现在,我必须在下一个POST调用主体中使用整个上面的RESPONSE,如下所示,但是,我想将"id"值更新为2,然后需要进行POST调用.
QUESTION: Now, I have to use entire above RESPONSE in next POST Call body as below, BUT, I wanted to update "id" value as 2 and then need to POST rest call.
REST CALL:------>"/check1/id2/post" 发布身体如下:------->
REST CALL: ------ > "/check1/id2/post" POST BODY as below : ------->
{
"test":"rest",
"check" :{
"id":2,
"name": "xyz"
"status":"updated"
}
}
================================================ =============
=============================================================
任何人都可以对此进行指导吗? ,对于如何解决此问题一无所知?,我需要使用Jmeter解决.
Can anyone please guide on this? , I am clueless about how to solve this issue?, I need to solve this using Jmeter.
推荐答案
您可以使用 JSR223预处理器
假设有效的JSON
{
"test":"rest",
"check" :{
"id":1,
"name": "xyz",
"status":"updated"
}
}
以下是步骤
- 将JSR223预处理器添加到第二个发布请求中.
-
将以下代码添加到预处理器
- Add a JSR223 preprocessor to the 2nd post request.
Add the following code to the preprocessor
导入groovy.json.JsonBuilder
import groovy.json.JsonBuilder
导入groovy.json.JsonSlurper
import groovy.json.JsonSlurper
def slurped = new JsonSlurper().parse(prev.getResponseData())
def slurped = new JsonSlurper().parse(prev.getResponseData())
def builder =新的JsonBuilder(含糊) builder.content.check.id ='2' vars.put("POSTDATA",builder.toPrettyString())
def builder = new JsonBuilder(slurped) builder.content.check.id = '2' vars.put("POSTDATA",builder.toPrettyString())
以上代码将ID值更新为2并将JSON存储在POSTDATA中,您可以使用${POSTDATA}
发布JSON FILE
Above code will update ID value with 2 and stores the JSON in POSTDATA , you can use ${POSTDATA}
to post the JSON FILE
在我的预处理器中,我使用prev.getResponseData()
保存响应,这意味着该预处理器必须随第一个采样器的旁边的采样器一起提供.
In my pre processor i'm saving the response using prev.getResponseData()
, that means this pre processor must included with the sampler right next to the first sampler.
有关beanshell的更多信息,请遵循此链接
For more information on beanshell Please follow this link
这篇关于如何使用Jmeter存储整个响应并为下一次休息呼叫更新它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!