Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An OkHttp interceptor that logs requests as curl shell commands
/*
* 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);
}
}
@j2emanue

This comment has been minimized.

Copy link

commented Jan 19, 2016

What if i did a POST request, would i see the data in the body of the request or just the url of the request ?

@avenwu

This comment has been minimized.

Copy link

commented Jan 20, 2016

Nice idea

@lupsyn

This comment has been minimized.

Copy link

commented Jan 20, 2016

Great idea. Very useful if you don't use a proxy in order to see what is going in your client.

@akperkins

This comment has been minimized.

Copy link

commented Feb 20, 2016

I just implemented this in my app for debug builds. It was pretty painless except that I had to surround the request url with quotes to get the curl command to run properly on my terminal.

Awesome idea. Thanks!

@terrakok

This comment has been minimized.

Copy link

commented May 13, 2016

Awesome!

@Jeevuz

This comment has been minimized.

Copy link

commented Jun 7, 2016

Wery nice, thanks! 👍 Will be great to see this as library.

@Jeevuz

This comment has been minimized.

Copy link

commented Jun 7, 2016

I noticed the problem with If-None-Match header. It uses quotes(") at start and end of the value. And this must be escaped to work properly.
So I added this before line 74:

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

This comment has been minimized.

Copy link

commented Dec 7, 2016

This currently logs the request as it was build in the interceptor chain at that point. I found it to be more useful to first execute the request using chain.proceed(chain.request()) and then use response.request() to build the curl command. This way any headers and other data which is being added to the request after this interceptor is also displayed in the curl request.

@terrakok

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.