Skip to content

Instantly share code, notes, and snippets.

@silvestrpredko
Last active April 10, 2020 13:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save silvestrpredko/5e7f07fa4ef48bf414d6 to your computer and use it in GitHub Desktop.
Save silvestrpredko/5e7f07fa4ef48bf414d6 to your computer and use it in GitHub Desktop.
Append parameters to POST in Intercept(OkHttp)
/**
* @param parameter - this is string that contains parameters for Http POST
* @param request - old Request
* @return - new {@link Request} with additional parameters
* */
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment