简体   繁体   English

jmeter修改json GET结果并在PUT中使用

[英]jmeter modify json GET result and use in PUT

I'm using Jmeter to automate some tests cases. 我正在使用Jmeter来自动化一些测试用例。 I have a JSON response from a GET, I want to change a single value of that JSON response and use the modified response in the body of a PUT. 我有来自GET的JSON响应,我想更改该JSON响应的单个值,并在PUT的主体中使用修改后的响应。 The PUT needs all the same fields as the response, and I won't know what they all are, so I don't think the JSON Path Extractor will work in my case. PUT需要与响应相同的所有字段,我不知道它们都是什么,所以我认为JSON Path Extractor不适用于我的情况。 Efficiecy is a plus here, but I'll settle for something that just works. 效率在这里是一个加分,但我会满足于一些有效的东西。
I've tried this javascript in a BSF PostProcessor: 我在BSF PostProcessor中试过这个javascript:

var response = SampleResult.getResponseDataAsString();
var jsonOutput = JSON.parse(response);
jsonOutput.configState = "DELETED";
vars.put("json",jsonOutput);

But the Debug Sampler shows the response as an "Object" and nothing more. 但Debug Sampler将响应显示为“对象”,仅此而已。 Any ideas? 有任何想法吗? Thanks. 谢谢。

I don't think you have JSON in Rhino or Nashorn, that's why your script is failing. 我不认为你在Rhino或Nashorn中有JSON ,这就是你的脚本失败的原因。 I would suggest considering switching to JSR223 PostProcessor and use JSONBuilder and JSONSlurper like: 我建议考虑切换到JSR223 PostProcessor并使用JSONBuilderJSONSlurper,如:

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

def slurped = new JsonSlurper().parseText(SampleResult.getResponseDataAsString())
def builder = new JsonBuilder(slurped)
builder.content.configState = 'DELETED'
vars.put("json", builder.toPrettyString()) 

See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! 请参阅Beanshell vs JSR223 vs Java JMeter Scripting:Performance-Off你一直在等待! article to learn about 文章要了解

  • why JSR223 and Groovy is better than JavaScript 为什么JSR223和Groovy比JavaScript更好
  • how to add Groovy engine support to Jmeter 如何向Jmeter添加Groovy引擎支持
  • scripting best practices 脚本最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM