Search code examples
jmeterjmeter-plugins

String replace function issue


I used string replace function in jmeter script and was working fine when I ran the script on the local machine but when the same script was run on the server, it is showing an error.

The function used is : ${__strReplace(${C_Create_Escape},",\\\",)} where create escape is a regular expression.

On server showing 400 error not passing the string replace function.

Error:

      "timestamp":1547805846520,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Unexpected character ('\\' (code 92)): was expecting double-quote to start field name\n
     at [Source: java.io.PushbackInputStream@463eb3f3; line: 1, column: 2804] (through reference chain: 
com.acn.hps.gpp.gibs.dto.FormRequestDTO[\"gibsFormDTO\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('\\' (code 92)): was expecting double-quote to start field name\n at [Source: java.io.PushbackInputStream@463eb3f3; line: 1, column: 2804] (through reference chain: com.acn.hps.gpp.gibs.dto.FormRequestDTO[\"gibsFormDTO\"])","path":"/form/createOrEditForm"}

Solution

  • According to JMeter Documentation:

    If a function parameter contains a comma, then be sure to escape this with "\", otherwise JMeter will treat it as a parameter delimiter.

    This restriction has a side-effect: if you need to pass a backslash - you need to escape it with another backslash

    So basically if you pass "a" to your function you will get \"a\" as the result:

    enter image description here

    While your server expects it to be escaped with the double slash, to wit \\"a\\"

    My assumption is that you will need to add some more backslashes in order to be compliant with both JMeter Functions syntax and whatever your server expects. The resulting syntax would be:

    ${__strReplace(${C_Create_Escape},",\\\\\\\",)}
    

    enter image description here

    Check out Apache JMeter Functions - An Introduction article for more information on JMeter Functions concept.