字符串替换函数问题

String replace function issue

我在 jmeter 脚本中使用了字符串替换功能,当我在本地机器上 运行 脚本时工作正常,但是当相同的脚本在服务器上 运行 时,它显示错误.

使用的函数是:${__strReplace(${C_Create_Escape},",\\",)} 其中 create escape 是正则表达式。

服务器显示 400 错误,未通过字符串替换功能。

错误:

      "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"}

根据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.

这个限制有一个副作用:如果你需要传递一个反斜杠 - 你需要用另一个反斜杠转义它

所以基本上,如果您将 "a" 传递给您的函数,您将得到 \"a\" 作为结果:

虽然您的服务器希望它用双斜杠转义,但 \"a\"

我的假设是您将需要添加更多反斜杠以符合 JMeter Functions 语法和您的服务器期望的任何语法。生成的语法为:

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

查看 Apache JMeter Functions - An Introduction 文章以获取有关 JMeter 函数概念的更多信息。