Modify incremental post 2 for intercept request

I have it:

OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
    @Override
    public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        HttpUrl url = request.httpUrl().newBuilder()
                     .addQueryParameter("platform", "android")
                     .addQueryParameter("app_version", com.package.BuildConfig.VERSION_NAME)
                     .build();
        Request newRequest = chain.request().newBuilder().url(url).build();
        return chain.proceed(newRequest);
    }
});

but would also like to add an additional post key value to the request body containing the user key. It would look like

    RequestBody newBody = RequestBody.create(request.body().contentType(),request.body().content+ request.addPost("sUserKey","3254345kdskf");
...
...
 Request newRequest = chain.request()
.newBuilder()
.url(url)
.post(newBody)
.build();
+4
source share
2 answers

You can do this without creating an extra class.

client.interceptors().add(new Interceptor() {
    @Override
    public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        String parameter = "&" + name + "=" + value;
        Request newRequest = interceptRequest(request, parameter)
        return chain.proceed(newRequest);
    }
});

This is a simple method that creates a new request.

 public static Request interceptRequest(@NotNull Request request, @NotNull String parameter)
            throws IOException {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        Sink sink = Okio.sink(baos);
        BufferedSink bufferedSink = Okio.buffer(sink);

        /**
         * Write old params
         * */
        request.body().writeTo(bufferedSink);

        /**
         * write to buffer additional params
         * */
        bufferedSink.writeString(parameter, Charset.defaultCharset());

        RequestBody newRequestBody = RequestBody.create(
                request.body().contentType(),
                bufferedSink.buffer().readUtf8()
        );

        return request.newBuilder().post(newRequestBody).build();
    }

You can also get it from Gist

+3
source

RequestBody . RequestBody, post. - , , , . , , , POST. , . , , , URL, .

class AddPostParamRequestBody extends RequestBody {

    final RequestBody body;
    final String parameter;

    AddPostParamRequestBody(RequestBody body, String name, String value) {
        this.body = body;
        this.parameter = "&" + name + "=" + value;
    }

    @Override
    public long contentLength() throws IOException {
        return body.contentLength() + parameter.length();
    }

    @Override
    public MediaType contentType() {
        return body.contentType();
    }

    @Override
    public void writeTo(BufferedSink bufferedSink) throws IOException {
        body.writeTo(bufferedSink);
        bufferedSink.writeString(parameter, Charset.forName("UTF-8"));
    }

} 

-

client.interceptors().add(new Interceptor() {
    @Override
    public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        HttpUrl url = request.httpUrl().newBuilder().addQueryParameter("added", "param").build();
        AddPostParamRequestBody newBody = new AddPostParamRequestBody(request.body(), "sUserKey","3254345kdskf");
        Request newRequest = request.newBuilder().post(newBody).url(url).build();
        return chain.proceed(newRequest);
    }
});

, Field , , .

+5

Source: https://habr.com/ru/post/1614794/


All Articles