Java Code Examples for com.squareup.okhttp.Request#body()

The following examples show how to use com.squareup.okhttp.Request#body() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AllureOkHttp.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@Override
public Response intercept(final Chain chain) throws IOException {
    final AttachmentProcessor<AttachmentData> processor = new DefaultAttachmentProcessor();

    final Request request = chain.request();
    final String requestUrl = request.url().toString();
    final HttpRequestAttachment.Builder requestAttachmentBuilder = HttpRequestAttachment.Builder
            .create("Request", requestUrl)
            .setMethod(request.method())
            .setHeaders(toMapConverter(request.headers().toMultimap()));

    final RequestBody requestBody = request.body();
    if (Objects.nonNull(requestBody)) {
        requestAttachmentBuilder.setBody(readRequestBody(requestBody));
    }
    final HttpRequestAttachment requestAttachment = requestAttachmentBuilder.build();
    processor.addAttachment(requestAttachment, new FreemarkerAttachmentRenderer(requestTemplatePath));

    final Response response = chain.proceed(request);
    final HttpResponseAttachment.Builder responseAttachmentBuilder = HttpResponseAttachment.Builder
            .create("Response")
            .setResponseCode(response.code())
            .setHeaders(toMapConverter(response.headers().toMultimap()));

    final Response.Builder responseBuilder = response.newBuilder();

    final ResponseBody responseBody = response.body();

    if (Objects.nonNull(responseBody)) {
        final byte[] bytes = responseBody.bytes();
        responseAttachmentBuilder.setBody(new String(bytes, StandardCharsets.UTF_8));
        responseBuilder.body(ResponseBody.create(responseBody.contentType(), bytes));
    }

    final HttpResponseAttachment responseAttachment = responseAttachmentBuilder.build();
    processor.addAttachment(responseAttachment, new FreemarkerAttachmentRenderer(responseTemplatePath));

    return responseBuilder.build();
}
 
Example 2
Source File: SendFeedBack.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Response intercept(Chain chain) throws IOException {
    Request originalRequest = chain.request();
    if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
        return chain.proceed(originalRequest);
    }

    Request compressedRequest = originalRequest.newBuilder()
            .header("Content-Encoding", "gzip")
            .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body())))
            .build();
    return chain.proceed(compressedRequest);
}
 
Example 3
Source File: SendFeedBack.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Response intercept(Chain chain) throws IOException {
    Request originalRequest = chain.request();
    if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
        return chain.proceed(originalRequest);
    }

    Request compressedRequest = originalRequest.newBuilder()
            .header("Content-Encoding", "gzip")
            .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body())))
            .build();
    return chain.proceed(compressedRequest);
}
 
Example 4
Source File: OkHttpLogInterceptor.java    From wasp with Apache License 2.0 5 votes vote down vote up
@Override
public Response intercept(Chain chain) throws IOException {
  Request request = chain.request();
  Logger.d("---> REQUEST " + request.method() + " " + request.urlString());
  logHeaders(request.headers());
  //copy original request for logging request body
  Request copy = request.newBuilder().build();
  RequestBody requestBody = copy.body();
  if (requestBody == null) {
    Logger.d("Body - no body");
  } else {
    Buffer buffer = new Buffer();
    requestBody.writeTo(buffer);
    Logger.d("Body - " + buffer.readString(requestBody.contentType().charset()));
  }
  Logger.d("---> END");

  long t1 = System.nanoTime();
  Response response = chain.proceed(request);
  long t2 = System.nanoTime();

  Logger.d("<--- RESPONSE " + response.code() + " " + response.request().urlString());
  logHeaders(response.headers());
  String responseBody = response.body().string();
  Logger.d(TextUtils.isEmpty(responseBody) ? "Body - no body" : "Body - " + responseBody);
  Logger.d("<--- END " + "(Size: " + response.body().contentLength() + " bytes - "
      + "Network time: " + (t2 - t1) / MILLI_AS_NANO + " ms)");

  return response;
}
 
Example 5
Source File: ApiKeyAuth.java    From huaweicloud-cs-sdk with Apache License 2.0 4 votes vote down vote up
public Request applyToParams(Request request) {
    if (serviceName == null || region == null || accessKey == null || secretKey == null) {
        return request;
    }
    DefaultRequest reqForSigner = new DefaultRequest(this.serviceName);
    try {
        reqForSigner.setEndpoint(request.uri());

        reqForSigner.setHttpMethod(HttpMethodName.valueOf(request.method()));

        if(!projectId.isEmpty()) {
            reqForSigner.addHeader("X-Project-Id", projectId);
        }

        // add query string
        String urlString = request.urlString();
        if (urlString.contains("?")) {
            String parameters = urlString.substring(urlString.indexOf("?") + 1);
            Map<String, String> parametersMap = new HashMap<>();

            if (!parameters.isEmpty()) {
                for (String p : parameters.split("&")) {

                    String key = p.split("=")[0];
                    String value = p.split("=")[1];
                    parametersMap.put(key, value);
                }
                reqForSigner.setParameters(parametersMap);
            }
        }

        // add body
        if (request.body() != null) {
            Request copy = request.newBuilder().build();
            Buffer buffer = new Buffer();
            copy.body().writeTo(buffer);
            reqForSigner.setContent(new ByteArrayInputStream(buffer.readByteArray()));
        }

        Signer signer = SignerFactory.getSigner(serviceName, region);
        signer.sign(reqForSigner, new BasicCredentials(this.accessKey, this.secretKey));

        Request.Builder builder = request.newBuilder();
        builder.headers(Headers.of(reqForSigner.getHeaders()));
        return builder.build();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return request;
}
 
Example 6
Source File: ApiKeyAuth.java    From huaweicloud-cs-sdk with Apache License 2.0 4 votes vote down vote up
public Request applyToParams(Request request) {
    if (serviceName == null || region == null || accessKey == null || secretKey == null) {
        return request;
    }
    DefaultRequest reqForSigner = new DefaultRequest(this.serviceName);
    try {
        reqForSigner.setEndpoint(request.uri());

        reqForSigner.setHttpMethod(HttpMethodName.valueOf(request.method()));

        if(!projectId.isEmpty()) {
            reqForSigner.addHeader("X-Project-Id", projectId);
        }

        // add query string
        String urlString = request.urlString();
        if (urlString.contains("?")) {
            String parameters = urlString.substring(urlString.indexOf("?") + 1);
            Map<String, String> parametersMap = new HashMap<>();

            if (!parameters.isEmpty()) {
                for (String p : parameters.split("&")) {

                    String key = p.split("=")[0];
                    String value = p.split("=")[1];
                    parametersMap.put(key, value);
                }
                reqForSigner.setParameters(parametersMap);
            }
        }

        // add body
        if (request.body() != null) {
            Request copy = request.newBuilder().build();
            Buffer buffer = new Buffer();
            copy.body().writeTo(buffer);
            reqForSigner.setContent(new ByteArrayInputStream(buffer.readByteArray()));
        }

        Signer signer = SignerFactory.getSigner(serviceName, region);
        signer.sign(reqForSigner, new BasicCredentials(this.accessKey, this.secretKey));

        Request.Builder builder = request.newBuilder();
        builder.headers(Headers.of(reqForSigner.getHeaders()));
        return builder.build();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return request;
}