0
votes

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?

2

2 Answers

1
votes

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:

    enter image description here

0
votes

Replace this

sampler.getArguments().removeAllArguments()

with this

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