From fa54b5bc1f67da745757387a9cde162f5d035956 Mon Sep 17 00:00:00 2001 From: iroqueta Date: Tue, 27 Dec 2022 10:54:50 -0300 Subject: [PATCH 1/2] Sending Json data or XML data in a multi part request in HTTPClient do not send Content-Type in the data part Issue: 100250 --- .../com/genexus/internet/GXHttpClient.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/com/genexus/internet/GXHttpClient.java b/common/src/main/java/com/genexus/internet/GXHttpClient.java index 5e3d79ac9..195691af2 100644 --- a/common/src/main/java/com/genexus/internet/GXHttpClient.java +++ b/common/src/main/java/com/genexus/internet/GXHttpClient.java @@ -4,6 +4,14 @@ import HTTPClient.URI; import com.genexus.CommonUtil; import com.genexus.common.interfaces.SpecificImplementation; +import json.org.json.JSONException; +import json.org.json.JSONObject; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import java.io.*; import java.util.Hashtable; import java.util.Vector; @@ -776,8 +784,39 @@ String getHeaderTemplate(String name, String fileName, String mimeType){ return "Content-Disposition: form-data; name=\""+ name + "\"; filename=\""+ fileName + "\"\r\n" + "Content-Type: " + mimeType + "\r\n\r\n"; } String getFormDataTemplate(String varName, String value){ - return "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + varName + "\";\r\n\r\n" + value; + String contentType = getContentTypeFromString(value); + return "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + varName + "\";\r\n" + ((contentType != null)? "Content-Type: " + contentType + "\r\n" : "") + "\r\n" + value; } - } + private String getContentTypeFromString(String value){ + if (isJsonString(value)) + return "application/json"; + + if (isXMLString(value)) + return "text/xml"; + + return null; + } + + private boolean isJsonString(String value){ + try { + JSONObject json = new JSONObject(value); + return true; + } catch (JSONException e) { + return false; + } + } + + private boolean isXMLString(String value){ + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputSource inputSource = new InputSource(new StringReader(value)); + Document document = builder.parse(inputSource); + return true; + } catch (ParserConfigurationException | SAXException | IOException e) { + return false; + } + } + } } From 37e2e900306739d740aeee3a799487fb30976004 Mon Sep 17 00:00:00 2001 From: iroqueta Date: Wed, 28 Dec 2022 13:58:12 -0300 Subject: [PATCH 2/2] Sending Json data or XML data in a multi part request in HTTPClient do not send Content-Type in the data part Issue: 100250 --- common/src/main/java/com/genexus/internet/GXHttpClient.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/common/src/main/java/com/genexus/internet/GXHttpClient.java b/common/src/main/java/com/genexus/internet/GXHttpClient.java index 195691af2..c0e768b21 100644 --- a/common/src/main/java/com/genexus/internet/GXHttpClient.java +++ b/common/src/main/java/com/genexus/internet/GXHttpClient.java @@ -9,6 +9,8 @@ import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -812,6 +814,7 @@ private boolean isXMLString(String value){ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader(value)); + builder.setErrorHandler(new DefaultHandler()); Document document = builder.parse(inputSource); return true; } catch (ParserConfigurationException | SAXException | IOException e) {