Search in sources :

Example 16 with RequestBody

use of okhttp3.RequestBody in project retrofit by square.

the class RequestBuilderTest method multipartNullRemovesPart.

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

        //
        @Multipart
        //
        @POST("/foo/bar/")
        Call<ResponseBody> method(@Part("ping") String ping, @Part("fizz") String fizz) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "pong", null);
    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\"").contains("\r\npong\r\n--");
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 17 with RequestBody

use of okhttp3.RequestBody in project keywhiz by square.

the class KeywhizClient method httpPost.

private String httpPost(HttpUrl url, Object content) throws IOException {
    RequestBody body = RequestBody.create(JSON, mapper.writeValueAsString(content));
    Request request = new Request.Builder().url(url).post(body).addHeader(HttpHeaders.CONTENT_TYPE, JSON.toString()).build();
    return makeCall(request);
}
Also used : CreateSecretRequest(keywhiz.api.CreateSecretRequest) LoginRequest(keywhiz.api.LoginRequest) Request(okhttp3.Request) CreateGroupRequest(keywhiz.api.CreateGroupRequest) CreateClientRequest(keywhiz.api.CreateClientRequest) RequestBody(okhttp3.RequestBody)

Example 18 with RequestBody

use of okhttp3.RequestBody in project keywhiz by square.

the class AutomationClientResourceIntegrationTest method addClients.

@Test
public void addClients() throws Exception {
    CreateClientRequest request = new CreateClientRequest("User1");
    String requestJSON = mapper.writeValueAsString(request);
    RequestBody body = RequestBody.create(KeywhizClient.JSON, requestJSON);
    Request post = new Request.Builder().post(body).url(testUrl("/automation/clients")).addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON).addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
    Response httpResponse = mutualSslClient.newCall(post).execute();
    assertThat(httpResponse.code()).isEqualTo(200);
}
Also used : ClientDetailResponse(keywhiz.api.ClientDetailResponse) Response(okhttp3.Response) CreateClientRequest(keywhiz.api.CreateClientRequest) Request(okhttp3.Request) CreateClientRequest(keywhiz.api.CreateClientRequest) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 19 with RequestBody

use of okhttp3.RequestBody in project keywhiz by square.

the class ClientResourceTest method modifyClient_notFound.

@Test
public void modifyClient_notFound() throws Exception {
    ModifyClientRequestV2 request = ModifyClientRequestV2.forName("non-existent");
    RequestBody body = RequestBody.create(JSON, mapper.writeValueAsString(request));
    Request post = clientRequest("/automation/v2/clients/non-existent").post(body).build();
    Response httpResponse = mutualSslClient.newCall(post).execute();
    assertThat(httpResponse.code()).isEqualTo(404);
}
Also used : Response(okhttp3.Response) ModifyClientRequestV2(keywhiz.api.automation.v2.ModifyClientRequestV2) Request(okhttp3.Request) TestClients.clientRequest(keywhiz.TestClients.clientRequest) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 20 with RequestBody

use of okhttp3.RequestBody in project keywhiz by square.

the class ClientResourceTest method create.

Response create(CreateClientRequestV2 request) throws IOException {
    RequestBody body = RequestBody.create(JSON, mapper.writeValueAsString(request));
    Request post = clientRequest("/automation/v2/clients").post(body).build();
    return mutualSslClient.newCall(post).execute();
}
Also used : Request(okhttp3.Request) TestClients.clientRequest(keywhiz.TestClients.clientRequest) RequestBody(okhttp3.RequestBody)

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