I have a JSON response as follows,
我有一个JSON响应如下,
{ "name":"John Smith", "address": "#123\r\nRenault Road\r\n123456\r\n" }
{“name”:“John Smith”,“address”:“#123 \ r \ nRenault Road \ r \ n \ n123456 \ r \ n”}
and I need to extract the address and put it into my next POST request. I use the JSON Extractor as a post processing element and with $..address
I set the extracted address to the variable address
.
我需要提取地址并将其放入我的下一个POST请求中。我使用JSON Extractor作为后处理元素,使用$ ..地址我将提取的地址设置为变量地址。
In my next request, I use the following as the POST data,
在我的下一个请求中,我使用以下作为POST数据,
{ "id": "123456", "address": "${address}" }
{“id”:“123456”,“地址”:“$ {address}”}
So when I make the request I see the POST data as,
所以当我发出请求时,我看到POST数据为,
{ "id": "123456", "address": "#123 Renault Road 123456 " }
{“id”:“123456”,“地址”:“#123 Renault Road 123456”}
This breaks at my backend and nevertheless this payload is not identified as a valid JSON as well.
这在我的后端中断,然而这个有效负载也没有被识别为有效的JSON。
I want to make the request so that the POST data is as follow,
我想发出请求,以便POST数据如下,
{ "id": "123456", "address": "#123\r\nRenault Road\r\n123456\r\n" }
{“id”:“123456”,“address”:“#123 \ r \ nRenault Road \ r \ n \ n123456 \ r \ n”}
Any help to get this done is highly appreciated.
任何帮助完成这项工作的人都非常感谢。
1 个解决方案
#1
1
If you really need to send these line breaks as \r\n
you can use some scripting to convert them back.
如果您确实需要将这些换行符发送为\ r \ n,则可以使用某些脚本将其转换回来。
- Add Beanshell PreProcessor as a child of the "next request" HTTP Request Sampler
-
Put the following code into the PreProcessor's "Script" area:
将以下代码放入PreProcessor的“脚本”区域:
address = vars.get("address"); address = address.replaceAll("\\r\\n","\\\\r\\\\n"); vars.put("address", address);
将Beanshell PreProcessor添加为“下一个请求”HTTP请求采样器的子级
The above script will convert line breaks to the textual representation.
上面的脚本会将换行符转换为文本表示。
References:
-
vars
is a shorthand for JMeterVariables class instance, it provides read/write access to all JMeter Variables - String.replaceAll() - is the method of String class which comes out of box with Java SDK
- How to Use BeanShell: JMeter's Favorite Built-in Component - guide demonstrating how to use Java and JMeter APIs using Beanshell test elements to enhance your JMeter tests with scripting if required
vars是JMeterVariables类实例的简写,它提供对所有JMeter变量的读/写访问
String.replaceAll() - 是String类的方法,它与Java SDK一起开箱即用
如何使用BeanShell:JMeter最受欢迎的内置组件 - 指导演示如何使用Beanshell测试元素使用Java和JMeter API,以便在需要时使用脚本来增强JMeter测试
#1
1
If you really need to send these line breaks as \r\n
you can use some scripting to convert them back.
如果您确实需要将这些换行符发送为\ r \ n,则可以使用某些脚本将其转换回来。
- Add Beanshell PreProcessor as a child of the "next request" HTTP Request Sampler
-
Put the following code into the PreProcessor's "Script" area:
将以下代码放入PreProcessor的“脚本”区域:
address = vars.get("address"); address = address.replaceAll("\\r\\n","\\\\r\\\\n"); vars.put("address", address);
将Beanshell PreProcessor添加为“下一个请求”HTTP请求采样器的子级
The above script will convert line breaks to the textual representation.
上面的脚本会将换行符转换为文本表示。
References:
-
vars
is a shorthand for JMeterVariables class instance, it provides read/write access to all JMeter Variables - String.replaceAll() - is the method of String class which comes out of box with Java SDK
- How to Use BeanShell: JMeter's Favorite Built-in Component - guide demonstrating how to use Java and JMeter APIs using Beanshell test elements to enhance your JMeter tests with scripting if required
vars是JMeterVariables类实例的简写,它提供对所有JMeter变量的读/写访问
String.replaceAll() - 是String类的方法,它与Java SDK一起开箱即用
如何使用BeanShell:JMeter最受欢迎的内置组件 - 指导演示如何使用Beanshell测试元素使用Java和JMeter API,以便在需要时使用脚本来增强JMeter测试