Jmeter change the POST request when Json body contain branket and slash

假装没事ソ 提交于 2019-12-23 05:27:08

问题


I am new to Jmeter and I am now facing with an issue when post a request with Json body with Jmeter. My request has body as below:

{"id":"KpiFormData","entity":"[\n\t\"{\\\"Timeout\\\": \\\"10\\\",\\\"kpiType\\\": \\\"Marketing.KPI.Common, Marketing.KPI, Version=x.x.x.0, Culture=neutral, PublicKeyToken=3fafga352gts345\\\",\\\"widgetID\\\": \\\"KpiWidget_0\\\",\\\"Weight\\\": \\\"Medium\\\",\\\"CurrentContent\\\": \\\"${pageId}_${version}\\\"}\",\n\t\"{}\"\n]"}

I want to parameterize this request to chain it with my test plan, so I need to input parameter ${pageId}_${version} in this request. But when include these parameters, request is changed its format as below, it leads to 500 internal error when sending this request. POST data:

{"id":"KpiFormData","entity":"[\n\t\"{\\"Timeout\\": \\"10\\",\\"kpiType\\": \\"Marketing.KPI.Common, Marketing.KPI, Version=x.x.x.0, Culture=neutral, PublicKeyToken=3fafga352gts345\\",\\"widgetID\\": \\"KpiWidget_0\\",\\"Weight\\": \\"Medium\\",\\"CurrentContent\\": \\"193_273\\"}\",\n\t\"{}\"\n]"}

Please notice that original request contains \\\ but request included parameters now contains \\, this makes error. If I only put numbers instead of parameters, this error does not happen, and post request works successfully. I tried to change Json body by include extra \, but it doesn't work, Is there any suggestion or solution for this?


回答1:


Replace this

sampler.getArguments().removeAllArguments()

with this

def arguments = new org.apache.jmeter.config.Arguments();
sampler.setArguments(arguments);



回答2:


It looks like a JMeter bug, consider reporting it via JMeter Bugzilla

In the meantime you can work it around by avoiding inlining JMeter Variables into the request body and making the values substitution by JSR223 PreProcessor and Groovy language

  1. Change your request body to look like:

    {"id":"KpiFormData","entity":"[\n\t\"{\\\"Timeout\\\": \\\"10\\\",\\\"kpiType\\\": \\\"Marketing.KPI.Common, Marketing.KPI, Version=x.x.x.0, Culture=neutral, PublicKeyToken=3fafga352gts345\\\",\\\"widgetID\\\": \\\"KpiWidget_0\\\",\\\"Weight\\\": \\\"Medium\\\",\\\"CurrentContent\\\": \\\"pageId_version\\\"}\",\n\t\"{}\"\n]"}
    

    to wit change ${pageId}_${version} to pageId_version

  2. Add JSR223 PreProcessor as a child of the HTTP Request sampler which sends the above payload
  3. Put the following code into "Script" area:

    import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase
    def request = sampler.getArguments().getArgument(0).getValue()
    request = request.replace('pageId',vars.get('pageId')).replace('version', vars.get('version'))
    sampler.getArguments().removeAllArguments()
    sampler.addNonEncodedArgument('',request,'')
    sampler.setPostBodyRaw(true)
    
  4. That's it, you should be able to send the parameterized request normally now:



来源:https://stackoverflow.com/questions/59170591/jmeter-change-the-post-request-when-json-body-contain-branket-and-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!