Search in sources :

Example 1 with POST

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

the class RequestBuilderTest method multipartPartsShouldBeInOrder.

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

Example 2 with POST

use of retrofit2.http.POST in project iNGAGE by davis123123.

the class UserInfoHandler method enqueue.

public void enqueue(String username) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(logging);
    Log.i("STATE", "Retrofit url" + get_url);
    Retrofit retrofit = new Retrofit.Builder().client(httpClient.build()).addConverterFactory(GsonConverterFactory.create()).baseUrl(get_url).build();
    Interface service = retrofit.create(Interface.class);
    Call<ResponseBody> call = service.get(username);
    call.enqueue(new Callback<ResponseBody>() {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Log.i("STATE", "Retrofit response code: " + response.code());
            if (response.isSuccessful()) {
                Log.i("STATE", "Retrofit POST Success");
                try {
                    serverResponse = response.body().string();
                    callBackData.notifyChange(serverResponse);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i("Retrofit Error Code:", String.valueOf(response.code()));
                Log.i("Retrofit Error Body", response.errorBody().toString());
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.i("STATE", "Retrofit Failure");
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 3 with POST

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

the class RequestBuilderTest method fieldParamMapsConvertedToNullShouldError.

@Test
public void fieldParamMapsConvertedToNullShouldError() throws Exception {
    class Example {

        @FormUrlEncoded
        @POST("/query")
        Call<ResponseBody> queryPath(@FieldMap Map<String, String> a) {
            return null;
        }
    }
    Retrofit.Builder retrofitBuilder = new Retrofit.Builder().baseUrl("http://example.com").addConverterFactory(new NullObjectConverterFactory());
    Map<String, String> queryMap = Collections.singletonMap("kit", "kat");
    try {
        buildRequest(Example.class, retrofitBuilder, queryMap);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessageContaining("Field map value 'kat' converted to null by retrofit2.helpers.NullObjectConverterFactory$1 for key 'kit'.");
    }
}
Also used : NullObjectConverterFactory(retrofit2.helpers.NullObjectConverterFactory) FieldMap(retrofit2.http.FieldMap) 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) Test(org.junit.Test)

Example 4 with POST

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

the class RequestBuilderTest method malformedContentTypeParameterThrows.

@Test
public void malformedContentTypeParameterThrows() {
    class Example {

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

Example 5 with POST

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

the class RequestBuilderTest method multipartWithEncoding.

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