From 3a8dc9ee04706e894f9c5d17dfcbc9d73d9b4e81 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Wed, 5 Aug 2020 13:08:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=AF=BC=E5=85=A5kv=E7=B1=BB=E5=9E=8Bbody=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/api/parse/MsParser.java | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) 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); + } + } + } + } }