diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/curl/constants/CurlPatternConstants.java b/backend/services/api-test/src/main/java/io/metersphere/api/curl/constants/CurlPatternConstants.java index 0f9c1aa3ed..2102dbf7cf 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/curl/constants/CurlPatternConstants.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/curl/constants/CurlPatternConstants.java @@ -46,7 +46,7 @@ public interface CurlPatternConstants { * -d/--data 请求体 */ Pattern DEFAULT_HTTP_BODY_PATTERN = Pattern.compile("(?:--data|-d)\\s+(?:'([^']*)'|\"([^\"]*)\"|(\\S+))", Pattern.DOTALL); - Pattern DEFAULT_HTTP_BODY_PATTERN_KV = Pattern.compile("^([^=&]+=[^=&]+)(?:&[^=&]+=[^=&]+)*$", Pattern.DOTALL); + Pattern DEFAULT_HTTP_BODY_PATTERN_KV = Pattern.compile("^([^=&]+=[^=&]*)(?:&[^=&]+=[^=&]*)*$", Pattern.DOTALL); /** * --data-raw 请求体 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/curl/domain/CurlEntity.java b/backend/services/api-test/src/main/java/io/metersphere/api/curl/domain/CurlEntity.java index 0ec0cf3207..fdaaca64f9 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/curl/domain/CurlEntity.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/curl/domain/CurlEntity.java @@ -34,7 +34,7 @@ public class CurlEntity { /** * 请求体 */ - private Map body; + private Object body; private String bodyType; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpBodyHandler.java b/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpBodyHandler.java index 81ffb8104d..f76f654f40 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpBodyHandler.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpBodyHandler.java @@ -61,7 +61,7 @@ public class HttpBodyHandler extends CurlHandlerChain { } } - private Map parseDefaultBody(Matcher defaultMatcher, CurlEntity entity) { + private Object parseDefaultBody(Matcher defaultMatcher, CurlEntity entity) { String bodyStr = ""; if (defaultMatcher.group(1) != null) { //单引号数据 @@ -78,6 +78,10 @@ public class HttpBodyHandler extends CurlHandlerChain { entity.setBodyType(Body.BodyType.JSON.name()); return JSON.parseMap(bodyStr); } + if(isXML(bodyStr)){ + entity.setBodyType(Body.BodyType.XML.name()); + return bodyStr; + } //其他格式 a=b&c=d entity.setBodyType(Body.BodyType.WWW_FORM.name()); @@ -136,12 +140,12 @@ public class HttpBodyHandler extends CurlHandlerChain { return urlEncodeData; } - private Map parseRowBody(Matcher rowMatcher, CurlEntity entity) { + private Object parseRowBody(Matcher rowMatcher, CurlEntity entity) { String rawData = rowMatcher.group(1); if (isXML(rawData)) { entity.setBodyType(Body.BodyType.XML.name()); - return xml2json(rawData); + return rawData; } if (isJSON(rawData)) { @@ -168,9 +172,10 @@ public class HttpBodyHandler extends CurlHandlerChain { public static boolean isXML(String xmlStr) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setFeature("disallow-doctype-decl", false); - factory.setFeature("external-general-entities", false); - factory.setFeature("external-parameter-entities", false); + factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false); + factory.setFeature("http://xml.org/sax/features/external-general-entities", false); + factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xmlStr)); @@ -181,12 +186,4 @@ public class HttpBodyHandler extends CurlHandlerChain { } } - private Map xml2json(String xmlStr) { - try { - return XMLUtils.xmlStringToJson(xmlStr); - } catch (JSONException e) { - throw new MSException(Translator.get("curl_raw_content_is_invalid"), e); - } - } - } \ No newline at end of file