Skip to content

Commit

Permalink
Support overriding content type via explicit header.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Aug 19, 2014
1 parent e36d1eb commit b16c9f2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
12 changes: 6 additions & 6 deletions okhttp-apache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
<artifactId>okhttp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>mockwebserver</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand All @@ -35,6 +29,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>mockwebserver</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
import com.squareup.okhttp.RequestBody;
import java.io.IOException;
import okio.BufferedSink;
import org.apache.http.Header;
import org.apache.http.HttpEntity;

/** Adapts an {@link HttpEntity} to OkHttp's {@link RequestBody}. */
final class HttpEntityBody extends RequestBody {
private static final String DEFAULT_MEDIA_TYPE = "application/octet-stream";
private static final MediaType DEFAULT_MEDIA_TYPE = MediaType.parse("application/octet-stream");

private final HttpEntity entity;
private final MediaType mediaType;

HttpEntityBody(HttpEntity entity) {
HttpEntityBody(HttpEntity entity, String contentTypeHeader) {
this.entity = entity;

// Apache is forgiving and lets you skip specifying a content type with an entity. OkHttp is
// not forgiving so we fall back to a generic type if it's missing.
Header contentType = entity.getContentType();
mediaType = MediaType.parse(contentType != null ? contentType.getValue() : DEFAULT_MEDIA_TYPE);
if (contentTypeHeader != null) {
mediaType = MediaType.parse(contentTypeHeader);
} else if (entity.getContentType() != null) {
mediaType = MediaType.parse(entity.getContentType().getValue());
} else {
// Apache is forgiving and lets you skip specifying a content type with an entity. OkHttp is
// not forgiving so we fall back to a generic type if it's missing.
mediaType = DEFAULT_MEDIA_TYPE;
}
}

@Override public long contentLength() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,22 @@ private static Request transformRequest(HttpRequest request) {
String method = requestLine.getMethod();
builder.url(requestLine.getUri());

String contentType = null;
for (Header header : request.getAllHeaders()) {
builder.header(header.getName(), header.getValue());
String name = header.getName();
if ("Content-Type".equals(name)) {
contentType = header.getValue();
} else {
builder.header(name, header.getValue());
}
}

RequestBody body = null;
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null) {
// Wrap the entity in a custom Body which takes care of the content, length, and type.
body = new HttpEntityBody(entity);
body = new HttpEntityBody(entity, contentType);

Header encoding = entity.getContentEncoding();
if (encoding != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -114,6 +115,19 @@ public class OkApacheClientTest {
assertEquals(request.getHeader("Content-Length"), "13");
}

@Test public void postOverrideContentType() throws Exception {
server.enqueue(new MockResponse());

HttpPost httpPost = new HttpPost();
httpPost.setURI(server.getUrl("/").toURI());
httpPost.addHeader("Content-Type", "application/xml");
httpPost.setEntity(new StringEntity("<yo/>"));
client.execute(httpPost);

RecordedRequest request = server.takeRequest();
assertEquals(request.getHeader("Content-Type"), "application/xml");
}

@Test public void contentType() throws Exception {
server.enqueue(new MockResponse().setBody("<html><body><h1>Hello, World!</h1></body></html>")
.setHeader("Content-Type", "text/html"));
Expand Down

0 comments on commit b16c9f2

Please sign in to comment.