diff --git a/backend/src/main/java/io/metersphere/api/parse/MsParser.java b/backend/src/main/java/io/metersphere/api/parse/MsParser.java index 0b77a2c126..9df5c21e9e 100644 --- a/backend/src/main/java/io/metersphere/api/parse/MsParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/MsParser.java @@ -11,7 +11,6 @@ import org.apache.commons.lang3.StringUtils; import org.eclipse.jetty.http.HttpMethod; import java.io.InputStream; -import java.util.Map; public class MsParser extends ApiImportAbstractParser { @@ -49,18 +48,7 @@ public class MsParser extends ApiImportAbstractParser { requestObject.put(key, requestTmpObject.get(key)); });; requestObject.put("name", requestName); - JSONArray bodies = requestObject.getJSONArray("body"); - if (StringUtils.equalsIgnoreCase(requestObject.getString("method"), HttpMethod.POST.name()) && bodies != null) { - StringBuilder bodyStr = new StringBuilder(); - for (int i = 0; i < bodies.size(); i++) { - String body = bodies.getString(i); - bodyStr.append(body); - } - JSONObject bodyObject = new JSONObject(); - bodyObject.put("raw", bodyStr); - bodyObject.put("type", MsRequestBodyType.RAW.value()); - requestObject.put("body", bodyObject); - } + parseBody(requestObject); requestsObjects.add(requestObject); }); scenario.put("requests", requestsObjects); @@ -71,4 +59,39 @@ public class MsParser extends ApiImportAbstractParser { return result.toJSONString(); } } + + private void parseBody(JSONObject requestObject) { + if (requestObject.containsKey("body")) { + Object body = requestObject.get("body"); + if (body instanceof JSONArray) { + JSONArray bodies = requestObject.getJSONArray("body"); + if (StringUtils.equalsIgnoreCase(requestObject.getString("method"), HttpMethod.POST.name()) && bodies != null) { + StringBuilder bodyStr = new StringBuilder(); + for (int i = 0; i < bodies.size(); i++) { + String tmp = bodies.getString(i); + bodyStr.append(tmp); + } + JSONObject bodyObject = new JSONObject(); + bodyObject.put("raw", bodyStr); + bodyObject.put("type", MsRequestBodyType.RAW.value()); + requestObject.put("body", bodyObject); + } + } else if (body instanceof JSONObject) { + JSONObject bodyObj = requestObject.getJSONObject("body"); + if (StringUtils.equalsIgnoreCase(requestObject.getString("method"), HttpMethod.POST.name()) && bodyObj != null) { + JSONArray kvs = new JSONArray(); + bodyObj.keySet().forEach(key -> { + JSONObject kv = new JSONObject(); + kv.put("name", key); + kv.put("value", bodyObj.getString(key)); + kvs.add(kv); + }); + JSONObject bodyRes = new JSONObject(); + bodyRes.put("kvs", kvs); + bodyRes.put("type", MsRequestBodyType.KV.value()); + requestObject.put("body", bodyRes); + } + } + } + } }