Skip to content

Fix Response Charset to UTF-8. #7

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

Merged
merged 3 commits into from
Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Merged commit for Upgrade issue6 branch against the latest version 2.…
…3.3.
  • Loading branch information
kikutaro committed Aug 9, 2016
commit 753400d138e40ff21bb8fc4ace0ee98a19b139d8
48 changes: 12 additions & 36 deletions src/main/java/com/sendgrid/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.http.Header;
import org.apache.http.StatusLine;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -24,27 +25,6 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.apache.http.util.EntityUtils;

// Hack to get DELETE to accept a request body
@NotThreadSafe
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
Expand Down Expand Up @@ -136,23 +116,19 @@ public URI buildUri(String baseUri, String endpoint, Map<String, String> queryPa
return uri;
}

/**
* Prepare a Response object from an API call via Apache's HTTP client.
*
* @param response from a call to a CloseableHttpClient
*/
public Response getResponse(CloseableHttpResponse response) throws IOException {
String responseBody = "";
/**
* Prepare a Response object from an API call via Apache's HTTP client.
*
* @param response
* from a call to a CloseableHttpClient
*/
public Response getResponse(CloseableHttpResponse response) throws IOException {
ResponseHandler<String> handler = new SendGridResponseHandler();
String responseBody = "";

int statusCode = response.getStatusLine().getStatusCode();

try {
if(response.getEntity() != null) {
responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}
} catch (IOException ex) {
throw ex;
}
responseBody = handler.handleResponse(response);

Header[] headers = response.getAllHeaders();
Map<String, String> responseHeaders = new HashMap<String, String>();
Expand Down Expand Up @@ -350,4 +326,4 @@ public Response api(Request request) throws IOException {
throw new IOException(errors.toString());
}
}
}
}
9 changes: 5 additions & 4 deletions src/main/java/com/sendgrid/SendGridResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.http.client.HttpResponseException;
import org.apache.http.impl.client.AbstractResponseHandler;
import org.apache.http.util.EntityUtils;
import java.nio.charset.StandardCharsets;

/**
* A {@link org.apache.http.client.ResponseHandler} that returns the response body as a String
Expand Down Expand Up @@ -34,9 +35,9 @@ public String handleResponse(final HttpResponse response)
return entity == null ? null : handleEntity(entity);
}

@Override
public String handleEntity(HttpEntity entity) throws IOException {
return EntityUtils.toString(entity);
}
@Override
public String handleEntity(HttpEntity entity) throws IOException {
return EntityUtils.toString(entity, StandardCharsets.UTF_8);
}

}
You are viewing a condensed version of this merge commit. You can view the full changes here.