Search in sources :

Example 1 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project Manhua by ag2s20150909.

the class GzipRequestInterceptor method gzip.

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            // 无法知道压缩后的数据大小
            return -1;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
Also used : GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink) RequestBody(okhttp3.RequestBody)

Example 2 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project lzc_app_lib by httplzc.

the class OkHttpCallFactory method transToBody.

private RequestBody transToBody(MultipartFormParams params) {
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    for (Map.Entry<String, String> entry : params.getUrlParams().entrySet()) {
        multipartBuilder.addFormDataPart(entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, MultipleData[]> entry : params.getMultipartDatas().entrySet()) {
        if (entry.getValue() != null) {
            for (MultipleData multipleData : entry.getValue()) {
                RequestBody requestBody = transToBody(multipleData.binaryData);
                if (requestBody != null)
                    multipartBuilder.addFormDataPart(entry.getKey(), multipleData.name, requestBody);
            }
        }
    }
    return multipartBuilder.build();
}
Also used : MultipartBody(okhttp3.MultipartBody) MultipleData(pers.lizechao.android_lib.net.params.MultipleData) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Example 3 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project lzc_app_lib by httplzc.

the class OkHttpCallFactory method transToRequest.

private Request transToRequest(RequestData requestData) {
    String url = requestData.url;
    BaseParams params = requestData.params;
    HttpMethod method = requestData.method;
    HttpUrl httpUrl = HttpUrl.parse(url);
    Objects.requireNonNull(httpUrl);
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.headers(Headers.of(params.getHeads()));
    RequestBody body = null;
    if (method == HttpMethod.GET || method == HttpMethod.HEAD) {
        if (!(params instanceof FormParams))
            throw new IllegalArgumentException(method.name() + "只能为表单数据");
        httpUrl = paramsToHttpUrl(httpUrl, (FormParams) params);
    } else {
        if (params instanceof RawParams) {
            body = transToBody(((RawParams) params).getBinaryData());
        } else if (params instanceof MultipartFormParams) {
            body = transToBody((MultipartFormParams) params);
        } else if (params instanceof FormParams) {
            body = transToBody((FormParams) params);
        }
    }
    if (body != null)
        body = new ProxyRequestBody(body, requestData.uploadProgress);
    return requestBuilder.url(httpUrl).method(method.name(), body).build();
}
Also used : MultipartFormParams(pers.lizechao.android_lib.net.params.MultipartFormParams) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) FormParams(pers.lizechao.android_lib.net.params.FormParams) MultipartFormParams(pers.lizechao.android_lib.net.params.MultipartFormParams) RawParams(pers.lizechao.android_lib.net.params.RawParams) BaseParams(pers.lizechao.android_lib.net.params.BaseParams) HttpMethod(pers.lizechao.android_lib.net.base.HttpMethod) RequestBody(okhttp3.RequestBody)

Example 4 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project fineract by apache.

the class ThitsaWorksCreditBureauIntegrationWritePlatformServiceImpl method okHttpConnectionMethod.

@Transactional
@Override
@SuppressWarnings("deprecation")
public String okHttpConnectionMethod(String userName, String password, String subscriptionKey, String subscriptionId, String url, String token, File file, FormDataContentDisposition fileData, Long uniqueId, String nrcId, String process) {
    String reponseMessage = null;
    RequestBody requestBody = null;
    OkHttpClient client = new OkHttpClient();
    if (process.equals("UploadCreditReport")) {
        String fileName = fileData.getFileName();
        requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
        requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", fileName, requestBody).addFormDataPart("BODY", "formdata").addFormDataPart("userName", userName).build();
    } else if (process.equals("token")) {
        final MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        String jsonBody = "" + "BODY=x-www-form-urlencoded&\r" + "grant_type=password&\r" + "userName=" + userName + "&\r" + "password=" + password + "&\r";
        requestBody = RequestBody.create(jsonBody, mediaType);
    } else if (process.equals("NRC")) {
        final MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        String jsonBody = "BODY=x-www-form-urlencoded&nrc=" + nrcId + "&";
        requestBody = RequestBody.create(jsonBody, mediaType);
    }
    HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
    String urlokhttp = urlBuilder.build().toString();
    Request request = null;
    if (token == null) {
        request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "application/x-www-form-urlencoded").url(urlokhttp).post(requestBody).build();
    }
    if (token != null) {
        if (process.equals("CreditReport")) {
            // GET method for fetching credit report
            request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "application/x-www-form-urlencoded").header("Authorization", "Bearer " + token).url(urlokhttp).get().build();
        } else if (process.equals("UploadCreditReport")) {
            // POST for uploading Credit-Report(multipart/form-data)
            // To ThitsaWork
            request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "multipart/form-data").header("Authorization", "Bearer " + token).url(urlokhttp).post(requestBody).build();
        } else {
            // POST method for application/x-www-form-urlencoded
            request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "application/x-www-form-urlencoded").header("Authorization", "Bearer " + token).url(urlokhttp).post(requestBody).build();
        }
    }
    Response response;
    Integer responseCode = 0;
    try {
        response = client.newCall(request).execute();
        responseCode = response.code();
        reponseMessage = response.body().string();
    } catch (IOException e) {
        LOG.error("error occured in HTTP request-response method.", e);
    }
    if (responseCode != HttpURLConnection.HTTP_OK) {
        this.httpResponse(responseCode, reponseMessage);
    }
    if (process.equals("UploadCreditReport")) {
        // to show the Response on frontEnd
        JsonObject reportObject = JsonParser.parseString(reponseMessage).getAsJsonObject();
        String ResponseMessageJson = reportObject.get("ResponseMessage").getAsString();
        this.handleAPIIntegrityIssues(ResponseMessageJson);
    }
    return reponseMessage;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) CommandWrapperBuilder(org.apache.fineract.commands.service.CommandWrapperBuilder) DataValidatorBuilder(org.apache.fineract.infrastructure.core.data.DataValidatorBuilder) Request(okhttp3.Request) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project gim-plugin by davidvorona.

the class HttpClient method broadcast.

/**
 * Makes an HTTP POST request to the broadcast endpoint at the
 * URL injected from the plugin config. The JSON data is sent
 * in the request body. Times out after 5 seconds.
 *
 * @param dataJson request data in JSON
 * @throws IOException if request fails
 */
public void broadcast(String dataJson) throws IOException {
    RequestBody body = RequestBody.create(JSON, dataJson);
    Request request = new Request.Builder().url(getBaseUrl() + "/broadcast/" + namespace).post(body).build();
    Response response = client.newCall(request).execute();
    ResponseBody bodyJson = response.body();
    if (bodyJson != null) {
        logHttpResponse(response.code(), bodyJson.string());
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody)

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