Search in sources :

Example 1 with FieldMap

use of retrofit2.http.FieldMap 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 2 with FieldMap

use of retrofit2.http.FieldMap 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 3 with FieldMap

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

the class RequestBuilderTest method formEncodedFieldMap.

@Test
public void formEncodedFieldMap() {
    class Example {

        //
        @FormUrlEncoded
        //
        @POST("/foo")
        Call<ResponseBody> method(@FieldMap Map<String, Object> fieldMap) {
            return null;
        }
    }
    Map<String, Object> fieldMap = new LinkedHashMap<>();
    fieldMap.put("kit", "kat");
    fieldMap.put("ping", "pong");
    Request request = buildRequest(Example.class, fieldMap);
    assertBody(request.body(), "kit=kat&ping=pong");
}
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)

Aggregations

HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Request (okhttp3.Request)3 ResponseBody (okhttp3.ResponseBody)3 Test (org.junit.Test)3 FieldMap (retrofit2.http.FieldMap)3 Map (java.util.Map)2 HeaderMap (retrofit2.http.HeaderMap)2 PartMap (retrofit2.http.PartMap)2 QueryMap (retrofit2.http.QueryMap)2 Buffer (okio.Buffer)1