Search in sources :

Example 11 with RequestBody

use of okhttp3.RequestBody 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 12 with RequestBody

use of okhttp3.RequestBody 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)

Example 13 with RequestBody

use of okhttp3.RequestBody in project retrofit by square.

the class RequestBuilderTest method put.

@Test
public void put() {
    class Example {

        //
        @PUT("/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("PUT");
    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 14 with RequestBody

use of okhttp3.RequestBody in project retrofit by square.

the class RequestBuilderTest method multipartOkHttpArrayPart.

@Test
public void multipartOkHttpArrayPart() throws IOException {
    class Example {

        //
        @Multipart
        //
        @POST("/foo/bar/")
        Call<ResponseBody> method(@Part MultipartBody.Part[] part) {
            return null;
        }
    }
    MultipartBody.Part part1 = MultipartBody.Part.createFormData("foo", "bar");
    MultipartBody.Part part2 = MultipartBody.Part.createFormData("kit", "kat");
    Request request = buildRequest(Example.class, new Object[] { new MultipartBody.Part[] { part1, part2 } });
    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=\"foo\"\r\n").contains("\r\nbar\r\n--");
    assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"kit\"\r\n").contains("\r\nkat\r\n--");
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) MultipartBody(okhttp3.MultipartBody) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 15 with RequestBody

use of okhttp3.RequestBody in project retrofit by square.

the class RequestBuilderTest method malformedContentTypeHeaderThrows.

@Test
public void malformedContentTypeHeaderThrows() {
    class Example {

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

Aggregations

RequestBody (okhttp3.RequestBody)178 Request (okhttp3.Request)141 IOException (java.io.IOException)75 Response (okhttp3.Response)74 Test (org.junit.Test)53 ResponseBody (okhttp3.ResponseBody)32 Call (okhttp3.Call)31 MultipartBody (okhttp3.MultipartBody)28 MediaType (okhttp3.MediaType)27 FormBody (okhttp3.FormBody)25 Callback (okhttp3.Callback)23 Buffer (okio.Buffer)23 MockResponse (okhttp3.mockwebserver.MockResponse)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 TestClients.clientRequest (keywhiz.TestClients.clientRequest)15 BufferedSink (okio.BufferedSink)15 Headers (okhttp3.Headers)12 HttpUrl (okhttp3.HttpUrl)11 JSONObject (org.json.JSONObject)11 Body (retrofit2.http.Body)11