@@ -97,6 +97,62 @@ public CloseableHttpResponse sendPOST(Map<Object, Object> map, String endpoint,
9797 return httpResponse ;
9898 }
9999
100+ public CloseableHttpResponse sendPOST (Map <Object , Object > map , Map <String , String > headers , String endpoint , String token , boolean withMediaFile ) throws IOException {
101+ HttpClientBuilder httpClientBuilder = getHttpClientBuilder ();
102+
103+ HttpClient httpClient = httpClientBuilder .build ();
104+
105+ System .out .println ("\n \n ------------------------------------------------------------" );
106+ System .out .println ("POST request to: " + url + endpoint );
107+ HttpPost request = new HttpPost (url + endpoint );
108+
109+ for (Map .Entry <String , String > entry : headers .entrySet ()) {
110+ request .addHeader (entry .getKey (), entry .getValue ());
111+ }
112+ String fileName = null ;
113+ File file = null ;
114+ if (withMediaFile ) {
115+ String boundary = "-------------" + System .currentTimeMillis ();
116+ fileName = (String ) map .get ("file" );
117+
118+ try {
119+ file = Helper .createFile (fileName );
120+
121+ } catch (AWTException e ) {
122+ e .printStackTrace ();
123+ }
124+
125+ MultipartEntityBuilder entity = MultipartEntityBuilder .create ()
126+ .setMode (HttpMultipartMode .BROWSER_COMPATIBLE )
127+ .setBoundary (boundary );
128+
129+ if (fileName != null ) {
130+ entity .addPart ("file" , new ByteArrayBody (Files .readAllBytes (file .toPath ()), fileName ));
131+ }
132+
133+ for (Map .Entry entry : map .entrySet ()){
134+ entity .addPart (String .valueOf (entry .getKey ()), new StringBody ((String ) entry .getValue ()));
135+ }
136+
137+
138+ request .setEntity (entity .build ());
139+ request .setHeader ("Content-type" , "multipart/form-data; boundary=" + boundary );
140+ } else {
141+ StringEntity params = new StringEntity (JSONObject .toJSONString (map ));
142+ System .out .println ("with data: " + JSONObject .toJSONString (map ));
143+ request .addHeader ("Content-Type" , "application/json;charset=UTF-8" );
144+ request .setEntity (params );
145+ }
146+
147+ CloseableHttpResponse httpResponse = (CloseableHttpResponse ) httpClient .execute (request );
148+
149+ if (file != null ) {
150+ file .delete ();
151+ }
152+
153+ return httpResponse ;
154+ }
155+
100156 private HttpClientBuilder getHttpClientBuilder () {
101157 SSLContext sslContext = null ;
102158 try {
@@ -189,6 +245,22 @@ protected Map<Integer, String> getPOST(Map map, String endpoint, String token, b
189245 return mapResult ;
190246 }
191247
248+ protected Map <Integer , String > getPOST (Map map , Map headers , String endpoint , String token , boolean withMediaFile ) throws IOException {
249+ Map <Integer , String > mapResult = new HashMap <Integer , String >();
250+
251+ CloseableHttpResponse response = sendPOST (map , headers , endpoint , token , withMediaFile );
252+
253+ StringBuffer body = getBodyResponse (response );
254+
255+ mapResult .put (response .getStatusLine ().getStatusCode (), body .toString ());
256+
257+ System .out .println ("\n \n Response:" );
258+ System .out .println (response .getStatusLine ());
259+ System .out .println (body .toString ());
260+
261+ return mapResult ;
262+ }
263+
192264 protected Map <Integer , String > getGET (String endpoint , String token ) throws IOException {
193265 Map <Integer , String > mapResult = new HashMap <Integer , String >();
194266
0 commit comments