Java Code Examples for retrofit2.http.Multipart

The following are top voted examples for showing how to use retrofit2.http.Multipart. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void onlyOneEncodingIsAllowedMultipartFirst() {
  class Example {
    @Multipart //
    @FormUrlEncoded //
    @POST("/") //
    Call<ResponseBody> method() {
      return null;
    }
  }
  try {
    buildRequest(Example.class);
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "Only one encoding annotation is allowed.\n    for method Example.method");
  }
}
 
Example 2
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void onlyOneEncodingIsAllowedFormEncodingFirst() {
  class Example {
    @FormUrlEncoded //
    @Multipart //
    @POST("/") //
    Call<ResponseBody> method() {
      return null;
    }
  }
  try {
    buildRequest(Example.class);
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "Only one encoding annotation is allowed.\n    for method Example.method");
  }
}
 
Example 3
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartFailsOnNonBodyMethod() {
  class Example {
    @Multipart //
    @GET("/") //
    Call<ResponseBody> method() {
      return null;
    }
  }
  try {
    buildRequest(Example.class);
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "Multipart can only be specified on HTTP methods with request body (e.g., @POST).\n    for method Example.method");
  }
}
 
Example 4
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartFailsWithNoParts() {
  class Example {
    @Multipart //
    @POST("/") //
    Call<ResponseBody> method() {
      return null;
    }
  }
  try {
    buildRequest(Example.class);
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "Multipart method must contain at least one @Part.\n    for method Example.method");
  }
}
 
Example 5
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartIterableRequiresName() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part List<RequestBody> part) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 6
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartArrayRequiresName() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part RequestBody[] part) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 7
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartOkHttpPartForbidsName() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("name") MultipartBody.Part part) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@Part parameters using the MultipartBody.Part must not include a part name in the annotation. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 8
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartIterableOkHttpPart() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("ping") List<MultipartBody.Part> part) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@Part parameters using the MultipartBody.Part must not include a part name in the annotation. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 9
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartArrayOkHttpPart() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("ping") MultipartBody.Part[] part) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@Part parameters using the MultipartBody.Part must not include a part name in the annotation. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 10
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartMapRejectsOkHttpPartValues() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap Map<String, MultipartBody.Part> parts) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@PartMap values cannot be MultipartBody.Part. Use @Part List<Part> or a different value type instead. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 11
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartMapRejectsNull() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap Map<String, RequestBody> parts) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage("Part map was null.");
  }
}
 
Example 12
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartMapRejectsNullValues() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap Map<String, RequestBody> parts) {
      return null;
    }
  }

  Map<String, RequestBody> params = new LinkedHashMap<>();
  params.put("ping", RequestBody.create(null, "pong"));
  params.put("kit", null);

  try {
    buildRequest(Example.class, params);
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage("Part map contained null value for key 'kit'.");
  }
}
 
Example 13
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartMapMustBeMap() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap List<Object> parts) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, Collections.emptyList());
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@PartMap parameter type must be Map. (parameter #1)\n    for method Example.method");
  }
}
 
Example 14
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartNullRemovesPart() throws IOException {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("ping") String ping, @Part("fizz") String fizz) {
      return null;
    }
  }
  Request request = buildRequest(Example.class, "pong", null);
  assertThat(request.method()).isEqualTo("POST");
  assertThat(request.headers().size()).isZero();
  assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");

  RequestBody body = request.body();
  Buffer buffer = new Buffer();
  body.writeTo(buffer);
  String bodyString = buffer.readUtf8();

  assertThat(bodyString)
      .contains("Content-Disposition: form-data;")
      .contains("name=\"ping\"")
      .contains("\r\npong\r\n--");
}
 
Example 15
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartsShouldBeInOrder() throws IOException {
  class Example {
    @Multipart
    @POST("/foo")
    Call<ResponseBody> get(@Part("first") String data, @Part("second") String dataTwo, @Part("third") String dataThree) {
      return null;
    }
  }
  Request request = buildRequest(Example.class, "firstParam", "secondParam", "thirdParam");
  MultipartBody body = (MultipartBody) request.body();

  Buffer buffer = new Buffer();
  body.writeTo(buffer);
  String readBody = buffer.readUtf8();

  assertThat(readBody.indexOf("firstParam")).isLessThan(readBody.indexOf("secondParam"));
  assertThat(readBody.indexOf("secondParam")).isLessThan(readBody.indexOf("thirdParam"));
}
 
Example 16
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void onlyOneEncodingIsAllowedFormEncodingFirst() {
  class Example {
    @FormUrlEncoded //
    @Multipart //
    @POST("/") //
    Call<ResponseBody> method() {
      return null;
    }
  }
  try {
    buildRequest(Example.class);
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "Only one encoding annotation is allowed.\n    for method Example.method");
  }
}
 
Example 17
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartFailsOnNonBodyMethod() {
  class Example {
    @Multipart //
    @GET("/") //
    Call<ResponseBody> method() {
      return null;
    }
  }
  try {
    buildRequest(Example.class);
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "Multipart can only be specified on HTTP methods with request body (e.g., @POST).\n    for method Example.method");
  }
}
 
Example 18
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartFailsWithNoParts() {
  class Example {
    @Multipart //
    @POST("/") //
    Call<ResponseBody> method() {
      return null;
    }
  }
  try {
    buildRequest(Example.class);
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "Multipart method must contain at least one @Part.\n    for method Example.method");
  }
}
 
Example 19
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartIterableRequiresName() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part List<RequestBody> part) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 20
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartArrayRequiresName() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part RequestBody[] part) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 21
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartOkHttpPartForbidsName() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("name") MultipartBody.Part part) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@Part parameters using the MultipartBody.Part must not include a part name in the annotation. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 22
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartIterableOkHttpPart() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("ping") List<MultipartBody.Part> part) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@Part parameters using the MultipartBody.Part must not include a part name in the annotation. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 23
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartArrayOkHttpPart() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("ping") MultipartBody.Part[] part) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@Part parameters using the MultipartBody.Part must not include a part name in the annotation. (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 24
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartMapRejectsNonStringKeys() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap Map<Object, RequestBody> parts) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@PartMap keys must be of type String: class java.lang.Object (parameter #1)\n"
            + "    for method Example.method");
  }
}
 
Example 25
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartMapRejectsNull() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap Map<String, RequestBody> parts) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage("Part map was null.");
  }
}
 
Example 26
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartMapRejectsNullKeys() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap Map<String, RequestBody> parts) {
      return null;
    }
  }

  Map<String, RequestBody> params = new LinkedHashMap<>();
  params.put("ping", RequestBody.create(null, "pong"));
  params.put(null, RequestBody.create(null, "kat"));

  try {
    buildRequest(Example.class, params);
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage("Part map contained null key.");
  }
}
 
Example 27
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartMapRejectsNullValues() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap Map<String, RequestBody> parts) {
      return null;
    }
  }

  Map<String, RequestBody> params = new LinkedHashMap<>();
  params.put("ping", RequestBody.create(null, "pong"));
  params.put("kit", null);

  try {
    buildRequest(Example.class, params);
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage("Part map contained null value for key 'kit'.");
  }
}
 
Example 28
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartMapMustBeMap() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap List<Object> parts) {
      return null;
    }
  }

  try {
    buildRequest(Example.class, Collections.emptyList());
    fail();
  } catch (IllegalArgumentException e) {
    assertThat(e).hasMessage(
        "@PartMap parameter type must be Map. (parameter #1)\n    for method Example.method");
  }
}
 
Example 29
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartMapSupportsSubclasses() throws IOException {
  class Foo extends HashMap<String, String> {
  }

  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap Foo parts) {
      return null;
    }
  }

  Foo foo = new Foo();
  foo.put("hello", "world");

  Request request = buildRequest(Example.class, foo);
  Buffer buffer = new Buffer();
  request.body().writeTo(buffer);
  assertThat(buffer.readUtf8())
      .contains("name=\"hello\"")
      .contains("\r\n\r\nworld\r\n--");
}
 
Example 30
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartNullRemovesPart() throws IOException {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("ping") String ping, @Part("fizz") String fizz) {
      return null;
    }
  }
  Request request = buildRequest(Example.class, "pong", null);
  assertThat(request.method()).isEqualTo("POST");
  assertThat(request.headers().size()).isZero();
  assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");

  RequestBody body = request.body();
  Buffer buffer = new Buffer();
  body.writeTo(buffer);
  String bodyString = buffer.readUtf8();

  assertThat(bodyString)
      .contains("Content-Disposition: form-data;")
      .contains("name=\"ping\"")
      .contains("\r\npong\r\n--");
}
 
Example 31
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 6 votes vote down vote up
@Test public void multipartPartsShouldBeInOrder() throws IOException {
  class Example {
    @Multipart
    @POST("/foo")
    Call<ResponseBody> get(@Part("first") String data, @Part("second") String dataTwo, @Part("third") String dataThree) {
      return null;
    }
  }
  Request request = buildRequest(Example.class, "firstParam", "secondParam", "thirdParam");
  MultipartBody body = (MultipartBody) request.body();

  Buffer buffer = new Buffer();
  body.writeTo(buffer);
  String readBody = buffer.readUtf8();

  assertThat(readBody.indexOf("firstParam")).isLessThan(readBody.indexOf("secondParam"));
  assertThat(readBody.indexOf("secondParam")).isLessThan(readBody.indexOf("thirdParam"));
}
 
Example 32
Project: retrofit-rxjava-request-with-progress   File: Api.java   Source Code and License 5 votes vote down vote up
@Multipart
@POST("pictureup/uploadshitu")
Observable<ProgressBean<String>> uploadPic(
        @Part("pos") String pos,
        @Part("uptype") String uptype,
        @Part("fm") String index,
        @Part("image\"; filename=\"xxx.jpg") File image
);
 
Example 33
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 5 votes vote down vote up
@Test public void multipartIterable() throws IOException {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("ping") List<String> ping) {
      return null;
    }
  }

  Request request = buildRequest(Example.class, Arrays.asList("pong1", "pong2"));
  assertThat(request.method()).isEqualTo("POST");
  assertThat(request.headers().size()).isZero();
  assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");

  RequestBody body = request.body();
  Buffer buffer = new Buffer();
  body.writeTo(buffer);
  String bodyString = buffer.readUtf8();

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

  assertThat(bodyString)
      .contains("Content-Disposition: form-data;")
      .contains("name=\"ping\"")
      .contains("\r\npong2\r\n--");
}
 
Example 34
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 5 votes vote down vote up
@Test public void multipartWithEncoding() throws IOException {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part(value = "ping", encoding = "8-bit") String ping,
        @Part(value = "kit", encoding = "7-bit") RequestBody kit) {
      return null;
    }
  }

  Request request = buildRequest(Example.class, "pong", RequestBody.create(
      MediaType.parse("text/plain"), "kat"));
  assertThat(request.method()).isEqualTo("POST");
  assertThat(request.headers().size()).isZero();
  assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");

  RequestBody body = request.body();
  Buffer buffer = new Buffer();
  body.writeTo(buffer);
  String bodyString = buffer.readUtf8();

  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("Content-Disposition: form-data;")
      .contains("name=\"kit\"")
      .contains("Content-Transfer-Encoding: 7-bit")
      .contains("\r\nkat\r\n--");
}
 
Example 35
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 5 votes vote down vote up
@Test public void multipartPartMapWithEncoding() throws IOException {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@PartMap(encoding = "8-bit") Map<String, RequestBody> parts) {
      return null;
    }
  }

  Map<String, RequestBody> params = new LinkedHashMap<>();
  params.put("ping", RequestBody.create(null, "pong"));
  params.put("kit", RequestBody.create(null, "kat"));

  Request request = buildRequest(Example.class, params);
  assertThat(request.method()).isEqualTo("POST");
  assertThat(request.headers().size()).isZero();
  assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");

  RequestBody body = request.body();
  Buffer buffer = new Buffer();
  body.writeTo(buffer);
  String bodyString = buffer.readUtf8();

  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("Content-Disposition: form-data;")
      .contains("name=\"kit\"")
      .contains("Content-Transfer-Encoding: 8-bit")
      .contains("\r\nkat\r\n--");
}
 
Example 36
Project: Tribe   File: ApiService.java   Source Code and License 5 votes vote down vote up
/**
 * 附近 模块 api
 */

//附近的人 改变在线状态
@Multipart
@POST("api.php?m=member&a=userStatus")
Observable<HttpResult<String>> setOnlineState(@Part("userid") String userid,
                                              @Part("type") String type);
 
Example 37
Project: lecrec-android   File: RecordService.java   Source Code and License 5 votes vote down vote up
@Multipart
@POST("records")
Call<Record> createRecord(
        @Header("Authorization") String token,
        @Part("title") String title,
        @Part("duration") String duration,
        @Part("filename") String filename,
        @Part("is_korean") Boolean isKorean,
        @Part("description") RequestBody description,
        @Part MultipartBody.Part file
);
 
Example 38
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 5 votes vote down vote up
@Test public void multipartPartOptional() {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("ping") RequestBody ping) {
      return null;
    }
  }
  try {
    buildRequest(Example.class, new Object[] { null });
    fail();
  } catch (IllegalStateException e) {
    assertThat(e.getMessage()).isEqualTo("Multipart body must have at least one part.");
  }
}
 
Example 39
Project: GitHub   File: ServiceMethod.java   Source Code and License 5 votes vote down vote up
private void parseMethodAnnotation(Annotation annotation) {
  if (annotation instanceof DELETE) {
    parseHttpMethodAndPath("DELETE", ((DELETE) annotation).value(), false);
  } else if (annotation instanceof GET) {
    parseHttpMethodAndPath("GET", ((GET) annotation).value(), false);
  } else if (annotation instanceof HEAD) {
    parseHttpMethodAndPath("HEAD", ((HEAD) annotation).value(), false);
    if (!Void.class.equals(responseType)) {
      throw methodError("HEAD method must use Void as response type.");
    }
  } else if (annotation instanceof PATCH) {
    parseHttpMethodAndPath("PATCH", ((PATCH) annotation).value(), true);
  } else if (annotation instanceof POST) {
    parseHttpMethodAndPath("POST", ((POST) annotation).value(), true);
  } else if (annotation instanceof PUT) {
    parseHttpMethodAndPath("PUT", ((PUT) annotation).value(), true);
  } else if (annotation instanceof OPTIONS) {
    parseHttpMethodAndPath("OPTIONS", ((OPTIONS) annotation).value(), false);
  } else if (annotation instanceof HTTP) {
    HTTP http = (HTTP) annotation;
    parseHttpMethodAndPath(http.method(), http.path(), http.hasBody());
  } else if (annotation instanceof retrofit2.http.Headers) {
    String[] headersToParse = ((retrofit2.http.Headers) annotation).value();
    if (headersToParse.length == 0) {
      throw methodError("@Headers annotation is empty.");
    }
    headers = parseHeaders(headersToParse);
  } else if (annotation instanceof Multipart) {
    if (isFormEncoded) {
      throw methodError("Only one encoding annotation is allowed.");
    }
    isMultipart = true;
  } else if (annotation instanceof FormUrlEncoded) {
    if (isMultipart) {
      throw methodError("Only one encoding annotation is allowed.");
    }
    isFormEncoded = true;
  }
}
 
Example 40
Project: GitHub   File: RequestBuilderTest.java   Source Code and License 5 votes vote down vote up
@Test public void simpleMultipart() throws IOException {
  class Example {
    @Multipart //
    @POST("/foo/bar/") //
    Call<ResponseBody> method(@Part("ping") String ping, @Part("kit") RequestBody kit) {
      return null;
    }
  }

  Request request = buildRequest(Example.class, "pong", RequestBody.create(
      MediaType.parse("text/plain"), "kat"));
  assertThat(request.method()).isEqualTo("POST");
  assertThat(request.headers().size()).isZero();
  assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");

  RequestBody body = request.body();
  Buffer buffer = new Buffer();
  body.writeTo(buffer);
  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--");
}