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"));
}
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");
}
});
}
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'.");
}
}
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!");
}
}
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--");
}
Aggregations