Search in sources :

Example 6 with POST

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

the class RequestBuilderTest method bodyWithPathParams.

@Test
public void bodyWithPathParams() {
    class Example {

        // 
        @POST("/foo/bar/{ping}/{kit}/")
        Call<ResponseBody> method(@Path("ping") String ping, @Body RequestBody body, @Path("kit") String kit) {
            return null;
        }
    }
    RequestBody body = RequestBody.create(TEXT_PLAIN, "Hi!");
    Request request = buildRequest(Example.class, "pong", body, "kat");
    assertThat(request.method()).isEqualTo("POST");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/pong/kat/");
    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)

Example 7 with POST

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

the class RequestBuilderTest method fieldMapSupportsSubclasses.

@Test
public void fieldMapSupportsSubclasses() throws IOException {
    class Foo extends HashMap<String, String> {
    }
    class Example {

        // 
        @FormUrlEncoded
        // 
        @POST("/")
        Call<ResponseBody> method(@FieldMap Foo a) {
            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()).isEqualTo("hello=world");
}
Also used : Buffer(okio.Buffer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FieldMap(retrofit2.http.FieldMap) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 8 with POST

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

the class RequestBuilderTest method post.

@Test
public void post() {
    class Example {

        // 
        @POST("/foo/bar/")
        Call<ResponseBody> method(@Body RequestBody body) {
            return null;
        }
    }
    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
    Request request = buildRequest(Example.class, body);
    assertThat(request.method()).isEqualTo("POST");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    assertBody(request.body(), "hi");
}
Also used : 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)

Example 9 with POST

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

the class RequestBuilderTest method formEncodedWithEncodedNameFieldParamMap.

@Test
public void formEncodedWithEncodedNameFieldParamMap() {
    class Example {

        // 
        @FormUrlEncoded
        // 
        @POST("/foo")
        Call<ResponseBody> method(@FieldMap(encoded = true) Map<String, Object> fieldMap) {
            return null;
        }
    }
    Map<String, Object> fieldMap = new LinkedHashMap<>();
    fieldMap.put("k%20it", "k%20at");
    fieldMap.put("pin%20g", "po%20ng");
    Request request = buildRequest(Example.class, fieldMap);
    assertBody(request.body(), "k%20it=k%20at&pin%20g=po%20ng");
}
Also used : FieldMap(retrofit2.http.FieldMap) Request(okhttp3.Request) PartMap(retrofit2.http.PartMap) HashMap(java.util.HashMap) HeaderMap(retrofit2.http.HeaderMap) FieldMap(retrofit2.http.FieldMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) QueryMap(retrofit2.http.QueryMap) ResponseBody(okhttp3.ResponseBody) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 10 with POST

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

the class RequestBuilderTest method multipartIterable.

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