Search in sources :

Example 11 with Multipart

use of retrofit2.http.Multipart in project retrofit by square.

the class RequestBuilderTest method simpleMultipart.

@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--");
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 12 with Multipart

use of retrofit2.http.Multipart in project retrofit by square.

the class RequestBuilderTest method multipartArray.

@Test
public void multipartArray() throws IOException {
    class Example {

        //
        @Multipart
        //
        @POST("/foo/bar/")
        Call<ResponseBody> method(@Part("ping") String[] ping) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, new Object[] { new String[] { "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--");
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 13 with Multipart

use of retrofit2.http.Multipart in project retrofit by square.

the class RequestBuilderTest method multipartPartMapSupportsSubclasses.

@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--");
}
Also used : Buffer(okio.Buffer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Request(okhttp3.Request) PartMap(retrofit2.http.PartMap) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

Request (okhttp3.Request)13 ResponseBody (okhttp3.ResponseBody)13 Buffer (okio.Buffer)13 Test (org.junit.Test)13 RequestBody (okhttp3.RequestBody)11 Part (retrofit2.http.Part)10 MultipartBody (okhttp3.MultipartBody)5 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 PartMap (retrofit2.http.PartMap)3 List (java.util.List)2 Map (java.util.Map)2 FieldMap (retrofit2.http.FieldMap)2 HeaderMap (retrofit2.http.HeaderMap)2 QueryMap (retrofit2.http.QueryMap)2