Search in sources :

Example 46 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project msgraph-sdk-java-core by microsoftgraph.

the class BaseClientTests method getStringFromRequestBody.

private String getStringFromRequestBody(Request request) {
    try {
        try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            final BufferedSink buffer = Okio.buffer(Okio.sink(out));
            final RequestBody body = request.body();
            if (body != null)
                body.writeTo(buffer);
            try (final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
                return CoreHttpProvider.streamToString(in);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        return "";
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedSink(okio.BufferedSink) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestBody(okhttp3.RequestBody)

Example 47 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project staf by simpleworks-gmbh.

the class HttpClient method buildRequestBody.

/**
 * @brief method that builds the request body for an HttpRequest
 * @param {@code HttpRequest} request
 * @retrun {@code RequestBody} request body, of the respecting request, null if
 *         method does not support request bodies
 */
private static RequestBody buildRequestBody(final HttpRequest request) {
    if (request == null) {
        throw new IllegalArgumentException("request can't be null.");
    }
    if (!request.getMethod().hasRequestBody()) {
        return null;
    }
    final RequestBody result;
    final ContentTypeEnum contenttype = request.getContentType();
    switch(contenttype) {
        case JSON:
            result = RequestBody.create(MediaType.parse(request.getContentType().getValue()), request.getBody());
            break;
        case FORM_URLENCODED:
            final Builder formBodyBuilder = new FormBody.Builder();
            for (FormParameter parameter : UtilsCollection.toList(request.getFormParameters())) {
                formBodyBuilder.add(parameter.getName(), parameter.getValue());
            }
            result = formBodyBuilder.build();
            break;
        case MULTIPART_FORM_DATA:
            final okhttp3.MultipartBody.Builder multipartBodyBuilder = new okhttp3.MultipartBody.Builder().setType(MultipartBody.FORM);
            for (final de.simpleworks.staf.commons.api.MultipartFormDataParameter multiPart : request.getMultipartFormDataParameters()) {
                final String name = multiPart.getName();
                final String value = multiPart.getValue();
                multipartBodyBuilder.addFormDataPart(name, value);
            }
            final MultipartFormFileParameter multipartFormFileParameter = request.getMultipartFormFileParameter();
            if (multipartFormFileParameter != null) {
                multipartBodyBuilder.addFormDataPart("file", multipartFormFileParameter.getName(), RequestBody.create(MediaType.parse(multipartFormFileParameter.getMimeType()), new File(multipartFormFileParameter.getFile())));
            }
            result = multipartBodyBuilder.build();
            break;
        default:
            if (HttpClient.logger.isDebugEnabled()) {
                HttpClient.logger.debug(String.format("Content Type '%s' is not implemented yet.", contenttype.getValue()));
                HttpClient.logger.debug("The Content Type was not defined, will return an empty request body.");
            }
            result = RequestBody.create(null, Convert.EMPTY_STRING);
    }
    return result;
}
Also used : ContentTypeEnum(de.simpleworks.staf.commons.enums.ContentTypeEnum) OkHttpBuilder(de.simpleworks.staf.framework.util.OkHttpBuilder) Builder(okhttp3.FormBody.Builder) FormParameter(de.simpleworks.staf.commons.api.FormParameter) MultipartFormFileParameter(de.simpleworks.staf.commons.api.MultipartFormFileParameter) MultipartBody(okhttp3.MultipartBody) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 48 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project msgraph-sdk-java-core by microsoftgraph.

the class RedirectHandlerTest method testGetRedirectForPostMethod.

@Test
public void testGetRedirectForPostMethod() {
    RedirectHandler redirectHandler = new RedirectHandler();
    RequestBody body = RequestBody.create("", MediaType.parse("application/json"));
    Request httppost = new Request.Builder().url(testurl).post(body).build();
    Response response = new Response.Builder().protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_MOVED_TEMP).message("Moved Temporarily").addHeader("location", testmeurl).request(httppost).build();
    try {
        Request request = redirectHandler.getRedirect(httppost, response);
        assertTrue(request != null);
        final String method = request.method();
        assertTrue(method.equalsIgnoreCase("POST"));
    } catch (ProtocolException e) {
        e.printStackTrace();
        fail("Redirect handler testGetRedirectForPostMethod failure");
    }
}
Also used : Response(okhttp3.Response) ProtocolException(java.net.ProtocolException) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody) Test(org.junit.jupiter.api.Test)

Example 49 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project java-sdk by smartcar.

the class Vehicle method stopCharge.

/**
 * Send request to the /charge endpoint to stop charging a vehicle
 *
 * @return a response indicating success
 * @throws SmartcarException if the request is unsuccessful
 */
public ActionResponse stopCharge() throws SmartcarException {
    JsonObject json = Json.createObjectBuilder().add("action", "STOP").build();
    RequestBody body = RequestBody.create(ApiClient.JSON, json.toString());
    return this.call("charge", "POST", body, ActionResponse.class);
}
Also used : JsonObject(javax.json.JsonObject) RequestBody(okhttp3.RequestBody)

Example 50 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project java-sdk by smartcar.

the class Vehicle method batch.

/**
 * Send request to the /batch endpoint
 *
 * @param paths the paths of endpoints to send requests to (ex. "/odometer", "/location", ...)
 * @return the BatchResponse object containing the response from all the requested endpoints
 * @throws SmartcarException if the request is unsuccessful
 */
public BatchResponse batch(String[] paths) throws SmartcarException {
    JsonArrayBuilder endpoints = Json.createArrayBuilder();
    for (String path : paths) {
        endpoints.add(Json.createObjectBuilder().add("path", path).build());
    }
    JsonArray requests = endpoints.build();
    JsonObject json = Json.createObjectBuilder().add("requests", requests).build();
    ApiClient.gson.registerTypeAdapter(BatchResponse.class, new BatchDeserializer());
    RequestBody body = RequestBody.create(ApiClient.JSON, json.toString());
    BatchResponse response = this.call("batch", "POST", body, BatchResponse.class);
    BatchResponse batchResponse = response;
    batchResponse.setRequestId(response.getMeta().getRequestId());
    return batchResponse;
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) JsonArrayBuilder(javax.json.JsonArrayBuilder) RequestBody(okhttp3.RequestBody)

Aggregations

RequestBody (okhttp3.RequestBody)1358 Request (okhttp3.Request)785 Response (okhttp3.Response)598 IOException (java.io.IOException)420 Test (org.junit.Test)235 OkHttpClient (okhttp3.OkHttpClient)216 MultipartBody (okhttp3.MultipartBody)213 MediaType (okhttp3.MediaType)204 Call (okhttp3.Call)198 JSONObject (org.json.JSONObject)183 ResponseBody (okhttp3.ResponseBody)177 Callback (okhttp3.Callback)115 FormBody (okhttp3.FormBody)106 Buffer (okio.Buffer)98 File (java.io.File)92 Map (java.util.Map)90 JsonObject (io.vertx.core.json.JsonObject)89 Headers (okhttp3.Headers)88 HashMap (java.util.HashMap)83 HttpUrl (okhttp3.HttpUrl)80