|
1 | 1 | package com.twilio.http;
|
2 | 2 |
|
3 | 3 | import com.twilio.Twilio;
|
| 4 | +import com.twilio.constant.EnumConstants; |
4 | 5 | import com.twilio.exception.ApiException;
|
| 6 | +import org.apache.http.HttpEntity; |
5 | 7 | import org.apache.http.HttpHeaders;
|
6 | 8 | import org.apache.http.HttpResponse;
|
7 | 9 | import org.apache.http.HttpVersion;
|
8 | 10 | import org.apache.http.client.config.RequestConfig;
|
9 | 11 | import org.apache.http.client.methods.RequestBuilder;
|
10 | 12 | import org.apache.http.config.SocketConfig;
|
| 13 | +import org.apache.http.entity.ContentType; |
| 14 | +import org.apache.http.entity.StringEntity; |
11 | 15 | import org.apache.http.impl.client.HttpClientBuilder;
|
12 | 16 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
13 | 17 | import org.apache.http.message.BasicHeader;
|
@@ -175,11 +179,20 @@ public Response makeRequest(Request request) {
|
175 | 179 |
|
176 | 180 | HttpMethod method = request.getMethod();
|
177 | 181 | if (method == HttpMethod.POST) {
|
178 |
| - builder.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); |
179 |
| - |
180 |
| - for (Map.Entry<String, List<String>> entry : request.getPostParams().entrySet()) { |
181 |
| - for (String value : entry.getValue()) { |
182 |
| - builder.addParameter(entry.getKey(), value); |
| 182 | + // TODO: It will be removed after one RC Release. |
| 183 | + if (request.getContentType() == null) request.setContentType(EnumConstants.ContentType.FORM_URLENCODED); |
| 184 | + if (EnumConstants.ContentType.JSON.getValue().equals(request.getContentType().getValue())) { |
| 185 | + HttpEntity entity = new StringEntity(request.getBody(), ContentType.APPLICATION_JSON); |
| 186 | + builder.setEntity(entity); |
| 187 | + builder.addHeader( |
| 188 | + HttpHeaders.CONTENT_TYPE, EnumConstants.ContentType.JSON.getValue()); |
| 189 | + } else { |
| 190 | + builder.addHeader( |
| 191 | + HttpHeaders.CONTENT_TYPE, EnumConstants.ContentType.FORM_URLENCODED.getValue()); |
| 192 | + for (Map.Entry<String, List<String>> entry : request.getPostParams().entrySet()) { |
| 193 | + for (String value : entry.getValue()) { |
| 194 | + builder.addParameter(entry.getKey(), value); |
| 195 | + } |
183 | 196 | }
|
184 | 197 | }
|
185 | 198 | }
|
|
0 commit comments