1

I need to write a middleware for OKHttp to intercept all sended query parameters (key1=value1&key2=value2&...) and generate a digest according to the parameters and then put it on a specific header and send it along with the request, I can intercept all request through the following way:

OkHttpClient httpClient = new OkHttpClient();  
httpClient.interceptors().add(new Interceptor() {  
    @Override
    public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
        Request original = chain.request();

        String digest = "How can I get sended paramters?";

        Request request = original.newBuilder()
            .header("User-Agent", "Your-App-Name")
            .header("Digest", digest)
            .method(original.method(), original.body())
            .build();

        return chain.proceed(request);
    }
});

But I can't find a way to retrieve the list of parameters! any ideas?

iSun
  • 1,714
  • 6
  • 28
  • 57

1 Answers1

2

It's been a while, but I believe you can just do:

Request original = chain.request();
String params = original.url().query();

don't have an android environment to test this with at the moment though. If not look at the okhttp javadoc for Request and HttpUrl

EDIT

for the post body there's a question here which I think does what you're after, but in a nutshell it's something like:

Request original = chain.request();
Buffer buffer = new Buffer();
original.body().writeTo(buffer);
String bodyStr = buffer.readUtf8();
Community
  • 1
  • 1
jonk
  • 1,494
  • 11
  • 13