diff --git a/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java b/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java index 6a796310fc..195e74224d 100644 --- a/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java +++ b/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java @@ -102,7 +102,6 @@ public class MockApiUtils { if (bodyObj.containsKey("raw")) { String xmlStr = bodyObj.getString("raw"); JSONObject matchObj = XMLUtils.XmlToJson(xmlStr); -// returnJsonArray.add(matchObj); returnJson = matchObj; } } else if (StringUtils.equalsIgnoreCase(type, "Raw")) { @@ -111,7 +110,6 @@ public class MockApiUtils { if(StringUtils.isNotEmpty(raw)){ JSONObject rawObject = new JSONObject(); rawObject.put("raw",raw); -// returnJsonArray.add(rawObject); returnJson = rawObject; } } @@ -134,7 +132,6 @@ public class MockApiUtils { bodyParamArr.put(kv.getString("name"), values); } } -// returnJsonArray.add(bodyParamArr); returnJson = bodyParamArr; } } @@ -388,11 +385,17 @@ public class MockApiUtils { JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0); for(String key : bodyParamObj.keySet()){ String value = String.valueOf(bodyParamObj.get(key)); + value = StringUtils.replace(value,"\\","\\\\"); + value = StringUtils.replace(value,"\"","\\\""); scriptStringBuffer.append("requestParams.put(\"body."+key+"\",\""+value+"\");\n"); if(StringUtils.equalsIgnoreCase(key,"raw")){ scriptStringBuffer.append("requestParams.put(\"bodyRaw\",\""+value+"\");\n"); } } + String jsonBody = bodyParamObj.toJSONString(); + jsonBody = StringUtils.replace(jsonBody,"\\","\\\\"); + jsonBody = StringUtils.replace(jsonBody,"\"","\\\""); + scriptStringBuffer.append("requestParams.put(\"body.json\",\""+jsonBody+"\");\n"); }else { scriptStringBuffer.append("requestParams.put(\"bodyRaw\",\""+requestMockParams.getBodyParams().toJSONString()+"\");\n"); } @@ -416,7 +419,6 @@ public class MockApiUtils { } return scriptStringBuffer.toString(); } - private static String runScript(String script, String scriptLanguage) throws ScriptException { JSR223Sampler jmeterScriptSampler = new JSR223Sampler(); jmeterScriptSampler.setScriptLanguage(scriptLanguage); diff --git a/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue b/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue index b277894760..5b526e7d05 100644 --- a/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue +++ b/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue @@ -54,7 +54,18 @@ export default { }, { title: this.$t('api_test.request.body')+this.$t('api_test.variable'), - value: 'var returnMsg = requestParams.get("body.${param}");\nreturn returnMsg;', + value: 'var returnMsg = requestParams.get("body.${param}");\nreturn returnMsg;\n' + + "\n"+ + '//如果对象是多层JSON,需要引入fastjson协助解析:\n' + + '// 以"{\"name\":\"user\",\"rows\":[{\"type\":1}]}" 为demo,取rows第1个的type数据:\n' + + 'import com.alibaba.fastjson.JSON;\n'+ + 'import com.alibaba.fastjson.JSONArray;\n'+ + 'import com.alibaba.fastjson.JSONObject;\n'+ + '\n'+ + 'var jsonParam = requestParams.get("body.json");\n' + + 'JSONObject jsonObject = JSONObject.parseObject(jsonParam);\n' + + 'var returnMsg = jsonObject.getJSONArray("rows").getJSONObject(0).getString("type");\n' + + 'return returnMsg;\n', }, { title: this.$t('api_test.request.body')+this.$t('api_test.variable')+" (Raw)",