0

I have a simple request which has an input in string format, having special char (quotes-dash-hash) etc

enter image description here While executing this request I am getting "Bad Request" error. I've tried setting up HTTP Header manager (content type) to 'application/json;charset=UTF-8'. But didn't work. Can someone please help?

Note: Actually, all the random song names fetched from CSV. Where 'normal song names' and 'song names having special char' string values are stored in column. So already tried 'all quoted data: true' as well in CSV Data Set Config.

My CVS looks like this:

  • Songname
  • Jack "50 Cent" Maddy: paul
  • Michael Jackson
  • Sean Paul
  • Nachi "TO infor"
  • Aln!!! E i Chips
Post req: /api/check/song?username=User8001
Header: 
Content-Type application/json;charset=UTF-8
Accept  application/xml;charset=UTF-8
Post body:
{
    "search": {
        "song": "${songname}",
       },
    "year": 2016    
}
Finch
  • 71
  • 1
  • 9
  • you need to escape `"` inside value as `"Jack \"50 Cent\" Maddy : Paul"` – Ori Marko Jul 27 '20 at 13:28
  • https://stackoverflow.com/users/7294900/user7294900: Okay. That is doable when you have single request but how to handle that when you have csv controlled data? any clue. And escaping the inside value may not serve the purpose :( – Finch Jul 27 '20 at 13:34
  • so how you JSON body really looks like ? – Ori Marko Jul 27 '20 at 13:38
  • https://stackoverflow.com/users/7294900/user7294900 The same as shown in snapshot. Thing is whatever comes as song value in Jason body , there is matching song in database which should get with their details as response. So the value Jack "50 Cent" Maddy: paul in json body returns "song writer: XYZ, rank: 345, lan: Spn" – Finch Jul 27 '20 at 13:44
  • Your server name is incorrect – Amol Chavan Jul 27 '20 at 14:27

1 Answers1

0

It's hard to come up with the comprehensive solution without seeing your source data from the CSV file and how you're building your HTTP Request sampler.

You're getting error because in JSON string literals you need to escape quotation marks with the backslash.

If this is something you cannot do in the CSV source data you could consider using i.e. StringEscapeUtils.escapeJavaScript() from the __groovy() function like:

${__groovy(org.apache.commons.lang.StringEscapeUtils.escapeJavaScript(vars.get('varFromCSV')),)}

Demo:

enter image description here

More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • https://stackoverflow.com/users/2897748/dmitri-t: I have tried your solution like this `"song": ${__groovy(org.apache.commons.lang.StringEscapeUtils.escapeJavaScript(vars.get('songname')),)}` but got this error **ERROR o.a.j.c.CSVDataSet: java.io.IOException: Cannot have quote-char in plain field:[50 Cent"]** **ERROR o.a.j.c.CSVDataSet: java.io.IOException: Cannot have quote-char in plain field:[Jack "]** Is there any other way or I did something wrong? – Finch Jul 28 '20 at 08:10