Skip to content

Commit

Permalink
Use form-data subtype for multipart requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaciko committed Sep 19, 2015
1 parent ea6fa8d commit e8431de
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions retrofit/src/main/java/retrofit/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ final class RequestBuilder {
} else if (isMultipart) {
// Will be set to 'body' in 'build'.
multipartBuilder = new MultipartBuilder();
multipartBuilder.type(MultipartBuilder.FORM);
}
}

Expand Down
2 changes: 1 addition & 1 deletion retrofit/src/main/java/retrofit/RequestBuilderAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ static final class PartMap extends RequestBuilderAction {
}

Headers headers = Headers.of(
"Content-Disposition", "name=\"" + entryKey + "\"",
"Content-Disposition", "form-data; name=\"" + entryKey + "\"",
"Content-Transfer-Encoding", transferEncoding);

Class<?> entryClass = entryValue.getClass();
Expand Down
2 changes: 1 addition & 1 deletion retrofit/src/main/java/retrofit/RequestFactoryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private void parseParameters(List<Converter.Factory> converterFactories) {
}
Part part = (Part) methodParameterAnnotation;
com.squareup.okhttp.Headers headers = com.squareup.okhttp.Headers.of(
"Content-Disposition", "name=\"" + part.value() + "\"",
"Content-Disposition", "form-data; name=\"" + part.value() + "\"",
"Content-Transfer-Encoding", part.encoding());
Converter<?, RequestBody> converter;
try {
Expand Down
2 changes: 1 addition & 1 deletion retrofit/src/main/java/retrofit/http/PartMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
@Target(PARAMETER)
@Retention(RUNTIME)
public @interface PartMap {
/** The {@code Content-Transfer-Encoding} of this part. */
/** The {@code Content-Transfer-Encoding} of the parts. */
String encoding() default "binary";
}
21 changes: 17 additions & 4 deletions retrofit/src/test/java/retrofit/RequestBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1179,10 +1179,12 @@ Call<Object> method(@Part("ping") String ping, @Part("kit") RequestBody kit) {
String bodyString = buffer.readUtf8();

assertThat(bodyString)
.contains("Content-Disposition: form-data;")
.contains("name=\"ping\"\r\n")
.contains("\r\npong\r\n--");

assertThat(bodyString)
.contains("Content-Disposition: form-data;")
.contains("name=\"kit\"")
.contains("\r\nkat\r\n--");
}
Expand All @@ -1208,11 +1210,15 @@ Call<Object> method(@Part(value = "ping", encoding = "8-bit") String ping,
body.writeTo(buffer);
String bodyString = buffer.readUtf8();

assertThat(bodyString).contains("name=\"ping\"\r\n")
assertThat(bodyString)
.contains("Content-Disposition: form-data;")
.contains("name=\"ping\"\r\n")
.contains("Content-Transfer-Encoding: 8-bit")
.contains("\r\npong\r\n--");

assertThat(bodyString).contains("name=\"kit\"")
assertThat(bodyString)
.contains("Content-Disposition: form-data;")
.contains("name=\"kit\"")
.contains("Content-Transfer-Encoding: 7-bit")
.contains("\r\nkat\r\n--");
}
Expand Down Expand Up @@ -1242,10 +1248,12 @@ Call<Object> method(@PartMap Map<String, Object> parts) {
String bodyString = buffer.readUtf8();

assertThat(bodyString)
.contains("Content-Disposition: form-data;")
.contains("name=\"ping\"\r\n")
.contains("\r\npong\r\n--");

assertThat(bodyString)
.contains("Content-Disposition: form-data;")
.contains("name=\"kit\"")
.contains("\r\nkat\r\n--");

Expand Down Expand Up @@ -1276,11 +1284,15 @@ Call<Object> method(@PartMap(encoding = "8-bit") Map<String, Object> parts) {
body.writeTo(buffer);
String bodyString = buffer.readUtf8();

assertThat(bodyString).contains("name=\"ping\"\r\n")
assertThat(bodyString)
.contains("Content-Disposition: form-data;")
.contains("name=\"ping\"\r\n")
.contains("Content-Transfer-Encoding: 8-bit")
.contains("\r\npong\r\n--");

assertThat(bodyString).contains("name=\"kit\"")
assertThat(bodyString)
.contains("Content-Disposition: form-data;")
.contains("name=\"kit\"")
.contains("Content-Transfer-Encoding: 8-bit")
.contains("\r\nkat\r\n--");

Expand Down Expand Up @@ -1345,6 +1357,7 @@ Call<Object> method(@Part("ping") String ping, @Part("fizz") String fizz) {
String bodyString = buffer.readUtf8();

assertThat(bodyString)
.contains("Content-Disposition: form-data;")
.contains("name=\"ping\"")
.contains("\r\npong\r\n--");
}
Expand Down

0 comments on commit e8431de

Please sign in to comment.