Skip to content

Commit c363b38

Browse files
authored
Sending Json data or XML data in a multi part request in HTTPClient … (#648)
* Sending Json data or XML data in a multi part request in HTTPClient do not send Content-Type in the data part Issue: 100250 * Sending Json data or XML data in a multi part request in HTTPClient do not send Content-Type in the data part Issue: 100250
1 parent 185c018 commit c363b38

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

common/src/main/java/com/genexus/internet/GXHttpClient.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
import HTTPClient.URI;
55
import com.genexus.CommonUtil;
66
import com.genexus.common.interfaces.SpecificImplementation;
7+
import json.org.json.JSONException;
8+
import json.org.json.JSONObject;
9+
import org.w3c.dom.Document;
10+
import org.xml.sax.InputSource;
11+
import org.xml.sax.SAXException;
12+
import org.xml.sax.helpers.DefaultHandler;
13+
14+
import javax.xml.parsers.DocumentBuilder;
15+
import javax.xml.parsers.DocumentBuilderFactory;
16+
import javax.xml.parsers.ParserConfigurationException;
717
import java.io.*;
818
import java.util.Hashtable;
919
import java.util.Vector;
@@ -776,8 +786,40 @@ String getHeaderTemplate(String name, String fileName, String mimeType){
776786
return "Content-Disposition: form-data; name=\""+ name + "\"; filename=\""+ fileName + "\"\r\n" + "Content-Type: " + mimeType + "\r\n\r\n";
777787
}
778788
String getFormDataTemplate(String varName, String value){
779-
return "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + varName + "\";\r\n\r\n" + value;
789+
String contentType = getContentTypeFromString(value);
790+
return "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + varName + "\";\r\n" + ((contentType != null)? "Content-Type: " + contentType + "\r\n" : "") + "\r\n" + value;
780791
}
781-
}
782792

793+
private String getContentTypeFromString(String value){
794+
if (isJsonString(value))
795+
return "application/json";
796+
797+
if (isXMLString(value))
798+
return "text/xml";
799+
800+
return null;
801+
}
802+
803+
private boolean isJsonString(String value){
804+
try {
805+
JSONObject json = new JSONObject(value);
806+
return true;
807+
} catch (JSONException e) {
808+
return false;
809+
}
810+
}
811+
812+
private boolean isXMLString(String value){
813+
try {
814+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
815+
DocumentBuilder builder = factory.newDocumentBuilder();
816+
InputSource inputSource = new InputSource(new StringReader(value));
817+
builder.setErrorHandler(new DefaultHandler());
818+
Document document = builder.parse(inputSource);
819+
return true;
820+
} catch (ParserConfigurationException | SAXException | IOException e) {
821+
return false;
822+
}
823+
}
824+
}
783825
}

0 commit comments

Comments
 (0)