Search in sources :

Example 11 with POST

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

the class RequestBuilderTest method multipartOkHttpArrayPart.

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

        // 
        @Multipart
        // 
        @POST("/foo/bar/")
        Call<ResponseBody> method(@Part MultipartBody.Part[] part) {
            return null;
        }
    }
    MultipartBody.Part part1 = MultipartBody.Part.createFormData("foo", "bar");
    MultipartBody.Part part2 = MultipartBody.Part.createFormData("kit", "kat");
    Request request = buildRequest(Example.class, new Object[] { new MultipartBody.Part[] { part1, part2 } });
    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=\"foo\"\r\n").contains("\r\nbar\r\n--");
    assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"kit\"\r\n").contains("\r\nkat\r\n--");
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) MultipartBody(okhttp3.MultipartBody) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 12 with POST

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

the class RequestBuilderTest method formEncodedFieldOptional.

@Test
public void formEncodedFieldOptional() {
    class Example {

        // 
        @FormUrlEncoded
        // 
        @POST("/foo")
        Call<ResponseBody> method(@Field("foo") String foo, @Field("ping") String ping, @Field("kit") String kit) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "bar", null, "kat");
    assertBody(request.body(), "foo=bar&kit=kat");
}
Also used : Field(retrofit2.http.Field) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 13 with POST

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

the class RequestBuilderTest method malformedContentTypeHeaderThrows.

@Test
public void malformedContentTypeHeaderThrows() {
    class Example {

        // 
        @POST("/")
        // 
        @Headers("Content-Type: hello, world!")
        Call<ResponseBody> method(@Body RequestBody body) {
            return null;
        }
    }
    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
    try {
        buildRequest(Example.class, body);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("Malformed content type: hello, world!\n" + "    for method Example.method");
    }
}
Also used : RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) MultipartBody(okhttp3.MultipartBody) Body(retrofit2.http.Body) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 14 with POST

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

the class RequestBuilderTest method multipartNullRemovesPart.

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

Example 15 with POST

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

the class RequestBuilderTest method normalPostWithPathParam.

@Test
public void normalPostWithPathParam() {
    class Example {

        // 
        @POST("/foo/bar/{ping}/")
        Call<ResponseBody> method(@Path("ping") String ping, @Body RequestBody body) {
            return null;
        }
    }
    RequestBody body = RequestBody.create(TEXT_PLAIN, "Hi!");
    Request request = buildRequest(Example.class, "pong", body);
    assertThat(request.method()).isEqualTo("POST");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/pong/");
    assertBody(request.body(), "Hi!");
}
Also used : Path(retrofit2.http.Path) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) MultipartBody(okhttp3.MultipartBody) Body(retrofit2.http.Body) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

ResponseBody (okhttp3.ResponseBody)40 Test (org.junit.Test)33 Request (okhttp3.Request)31 Response (retrofit2.Response)27 ServiceResponse (com.microsoft.rest.ServiceResponse)22 RequestBody (okhttp3.RequestBody)19 TypeToken (com.google.common.reflect.TypeToken)18 Product (fixtures.lro.models.Product)18 Buffer (okio.Buffer)14 MultipartBody (okhttp3.MultipartBody)13 Part (retrofit2.http.Part)10 OkHttpClient (okhttp3.OkHttpClient)8 Body (retrofit2.http.Body)8 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)7 Field (retrofit2.http.Field)6 FieldMap (retrofit2.http.FieldMap)6 PartMap (retrofit2.http.PartMap)6 List (java.util.List)5 Map (java.util.Map)5