Skip to content

Instantly share code, notes, and snippets.

@Jeevuz
Forked from jgilfelt/CurlLoggingInterceptor.java
Last active January 16, 2018 05:36
Show Gist options
  • Save Jeevuz/7cf803e56051a6f3bea6d7f6a7902454 to your computer and use it in GitHub Desktop.
Save Jeevuz/7cf803e56051a6f3bea6d7f6a7902454 to your computer and use it in GitHub Desktop.

Revisions

  1. Jeevuz revised this gist Mar 17, 2017. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions CurlLoggingInterceptor.java
    Original file line number Diff line number Diff line change
    @@ -12,19 +12,22 @@
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    *
    * Edited by Vasili Chyrvon: quotes(") in header handling and okhhtp3 use
    */

    import com.squareup.okhttp.Headers;
    import com.squareup.okhttp.Interceptor;
    import com.squareup.okhttp.MediaType;
    import com.squareup.okhttp.Request;
    import com.squareup.okhttp.RequestBody;
    import com.squareup.okhttp.Response;
    import com.squareup.okhttp.logging.HttpLoggingInterceptor.Logger;
    package ru.mobileup.igooods.network;

    import java.io.IOException;
    import java.nio.charset.Charset;

    import okhttp3.Headers;
    import okhttp3.Interceptor;
    import okhttp3.MediaType;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;
    import okhttp3.logging.HttpLoggingInterceptor.Logger;
    import okio.Buffer;

    /**
  2. Jeevuz revised this gist Jun 7, 2016. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions CurlLoggingInterceptor.java
    Original file line number Diff line number Diff line change
    @@ -71,6 +71,13 @@ public void setCurlOptions(String curlOptions) {
    for (int i = 0, count = headers.size(); i < count; i++) {
    String name = headers.name(i);
    String value = headers.value(i);

    int start = 0;
    int end = value.length() - 1;
    if (value.charAt(start) == '"' && value.charAt(end) == '"') {
    value = "\\\"" + value.substring(1, end) + "\\\"";
    }

    if ("Accept-Encoding".equalsIgnoreCase(name) && "gzip".equalsIgnoreCase(value)) {
    compressed = true;
    }
  3. @jgilfelt jgilfelt created this gist Jan 9, 2016.
    102 changes: 102 additions & 0 deletions CurlLoggingInterceptor.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    /*
    * Copyright (C) 2016 Jeff Gilfelt.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */

    import com.squareup.okhttp.Headers;
    import com.squareup.okhttp.Interceptor;
    import com.squareup.okhttp.MediaType;
    import com.squareup.okhttp.Request;
    import com.squareup.okhttp.RequestBody;
    import com.squareup.okhttp.Response;
    import com.squareup.okhttp.logging.HttpLoggingInterceptor.Logger;

    import java.io.IOException;
    import java.nio.charset.Charset;

    import okio.Buffer;

    /**
    * An OkHttp interceptor that logs requests as curl shell commands. They can then
    * be copied, pasted and executed inside a terminal environment. This might be
    * useful for troubleshooting client/server API interaction during development,
    * making it easy to isolate and share requests made by the app. <p> Warning: The
    * logs generated by this interceptor have the potential to leak sensitive
    * information. It should only be used in a controlled manner or in a
    * non-production environment.
    */
    public class CurlLoggingInterceptor implements Interceptor {

    private static final Charset UTF8 = Charset.forName("UTF-8");

    private final Logger logger;
    private String curlOptions;

    public CurlLoggingInterceptor() {
    this(Logger.DEFAULT);
    }

    public CurlLoggingInterceptor(Logger logger) {
    this.logger = logger;
    }

    /** Set any additional curl command options (see 'curl --help'). */
    public void setCurlOptions(String curlOptions) {
    this.curlOptions = curlOptions;
    }

    @Override public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    boolean compressed = false;

    String curlCmd = "curl";
    if (curlOptions != null) {
    curlCmd += " " + curlOptions;
    }
    curlCmd += " -X " + request.method();

    Headers headers = request.headers();
    for (int i = 0, count = headers.size(); i < count; i++) {
    String name = headers.name(i);
    String value = headers.value(i);
    if ("Accept-Encoding".equalsIgnoreCase(name) && "gzip".equalsIgnoreCase(value)) {
    compressed = true;
    }
    curlCmd += " -H " + "\"" + name + ": " + value + "\"";
    }

    RequestBody requestBody = request.body();
    if (requestBody != null) {
    Buffer buffer = new Buffer();
    requestBody.writeTo(buffer);
    Charset charset = UTF8;
    MediaType contentType = requestBody.contentType();
    if (contentType != null) {
    charset = contentType.charset(UTF8);
    }
    // try to keep to a single line and use a subshell to preserve any line breaks
    curlCmd += " --data $'" + buffer.readString(charset).replace("\n", "\\n") + "'";
    }

    curlCmd += ((compressed) ? " --compressed " : " ") + request.url();

    logger.log("╭--- cURL (" + request.url() + ")");
    logger.log(curlCmd);
    logger.log("╰--- (copy and paste the above line to a terminal)");

    return chain.proceed(request);
    }

    }