mrmike/Ok2Curl

Cookies added by CookieHandler don't show up as headers in the ok2curl-output

mknudsen opened this issue · 1 comments

In an app I bridge cookies between the WebView and okhttp. This is done by setting a cookie-handler on the okhttp-client. These cookies, however, do not show up as headers in the requests that are printed.

example cookie handler & request (httpbin here simply echos the cookies it receives).

public class Ok2CurlCookieDemo {

    static final String tag = "Ok2CurlCookieDemo";

    private Ok2CurlCookieDemo() {
    }

    public static class FooBarCookieHandler extends CookieHandler {

        @Override
        public Map<String, List<String>> get(final URI uri, final Map<String, List<String>> requestHeaders) throws IOException {
            final Map<String, List<String>> result = Maps.newHashMap();
            final ArrayList<String> value = Lists.newArrayList("foo=bar; banana=rama;");
            result.put("Cookie", value);
            return result;
        }

        @Override
        public void put(final URI uri, final Map<String, List<String>> responseHeaders) throws IOException {

        }
    }

    public static void doRequest() {

        final OkHttpClient okHttpClient = new OkHttpClient();

        okHttpClient.setCookieHandler(new FooBarCookieHandler());
        Ok2Curl.set(okHttpClient);

        okHttpClient
                .newCall(new Request.Builder().url("http://httpbin.org/cookies").build())
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(final Request request, final IOException e) {

                    }

                    @Override
                    public void onResponse(final Response response) throws IOException {
                        Log.d(tag, "httpbin got cookies: " +  response.body().string());
                    }
                });
    }
}

outputs:

10-16 10:39:49.350 1586-1608/? D/Ok2Curl: curl -X GET http://httpbin.org/cookies
10-16 10:39:49.556 1586-1608/? D/Ok2CurlCookieDemo: httpbin got cookies: {
10-16 10:39:49.556 1586-1608/? D/Ok2CurlCookieDemo:   "cookies": {
10-16 10:39:49.556 1586-1608/? D/Ok2CurlCookieDemo:     "banana": "rama", 
10-16 10:39:49.556 1586-1608/? D/Ok2CurlCookieDemo:     "foo": "bar"
10-16 10:39:49.556 1586-1608/? D/Ok2CurlCookieDemo:   }
10-16 10:39:49.556 1586-1608/? D/Ok2CurlCookieDemo: }

Hi @mknudsen

Thanks for reporting this issue and great example. Reason why Cookie header wasn't logged was the way how OkHttp add headers by CookieHandler. Solution for this is to use network interceptor and not standard application interceptor.

Steps to fix it:

  • Update lib to 0.0.4
  • Instead of calling Ok2Curl.set(okHttpClient) call okHttpClient.networkInterceptors().add(new CurlInterceptor())

I've also added tests covering this issue https://github.com/mrmike/Ok2Curl/blob/master/ok2curl/src/test/java/com/moczul/ok2curl/CookieHandlerTest.java
and new section in readme
https://github.com/mrmike/Ok2Curl/blob/master/README.md

I hope it helps.