diff --git a/common/src/main/java/com/genexus/internet/GXHttpClient.java b/common/src/main/java/com/genexus/internet/GXHttpClient.java index 5e3d79ac9..c0e768b21 100644 --- a/common/src/main/java/com/genexus/internet/GXHttpClient.java +++ b/common/src/main/java/com/genexus/internet/GXHttpClient.java @@ -4,6 +4,16 @@ 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 org.xml.sax.helpers.DefaultHandler; + +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 +786,40 @@ 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)); + builder.setErrorHandler(new DefaultHandler()); + Document document = builder.parse(inputSource); + return true; + } catch (ParserConfigurationException | SAXException | IOException e) { + return false; + } + } + } }