Content-Length: 385931 | pFad | http://github.com/twilio/twilio-java/pull/746/files

39 feat: support json content in requests by shrutiburman · Pull Request #746 · twilio/twilio-java · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support json content in requests #746

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/com/twilio/http/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Request {

private List<String> userAgentExtensions;

private String contentType;
/**
* Create a new API request.
*
Expand Down Expand Up @@ -113,6 +114,14 @@ public List<String> getUserAgentExtensions() {
return this.userAgentExtensions;
}

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

/**
* Create auth string from username and password.
*
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/twilio/http/ValidationClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.twilio.Twilio;
import com.twilio.exception.ApiException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
Expand Down Expand Up @@ -104,7 +107,14 @@ public Response makeRequest(Request request) {

HttpMethod method = request.getMethod();
if (method == HttpMethod.POST) {
builder.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
if(request.getContentType() != null && request.getContentType().equals("application/json")){
HttpEntity entity = new StringEntity(request.encodeFormBody(), ContentType.APPLICATION_JSON);
builder.setEntity(entity);
builder.addHeader(HttpHeaders.CONTENT_TYPE, request.getContentType());
}
else{
builder.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
}

for (Map.Entry<String, List<String>> entry : request.getPostParams().entrySet()) {
for (String value : entry.getValue()) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/twilio/http/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,10 @@ public void testEquals() {
assertNotEquals(request, new Object());
assertNotEquals(null, request);
}
@Test
public void testContentType() {
Request r = new Request(HttpMethod.POST, "http://example.com/foobar");
r.setContentType("application/json");
assertEquals("application/json", r.getContentType());
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/twilio/http/ValidationClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void testHttpGet() throws Exception {
@Test
public void testHttpPost() throws Exception {
exerciseHttpMethod(HttpMethod.POST);
testContentType(HttpMethod.POST);
}

@Test
Expand All @@ -46,6 +47,27 @@ private void exerciseHttpMethod(final HttpMethod httpMethod) throws Exception {
assertNotNull(validationHeaderValue);
assertTrue(validationHeaderValue.length() > 0);
}
private void testContentType(final HttpMethod httpMethod) throws Exception {
final KeyPair keyPair = generateKeyPair();
final MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody("{\n" +
" \"key1\": \"value1\",\n" +
" \"key2\": \"value2\"\n" +
"}"));
final String path = "/example123";
final HttpUrl url = server.url(path);
final ValidationClient client = new ValidationClient("dummy-sid1", "dummy-sid2", "dummy-signing-key", keyPair.getPrivate());
final Request request = new Request(httpMethod, url.url().toString());
request.setContentType("application/json");
final Response response = client.makeRequest(request);
assertEquals(200, response.getStatusCode());
final RecordedRequest recordedRequest = server.takeRequest();
assertEquals(httpMethod.name(), recordedRequest.getMethod());
assertEquals("application/json", recordedRequest.getHeader("Content-Type"));
final String validationHeaderValue = recordedRequest.getHeader("Twilio-Client-Validation");
assertNotNull(validationHeaderValue);
assertTrue(validationHeaderValue.length() > 0);
}

private static KeyPair generateKeyPair() throws Exception {
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/twilio/twilio-java/pull/746/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy