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