Last active
April 10, 2020 13:48
-
-
Save silvestrpredko/5e7f07fa4ef48bf414d6 to your computer and use it in GitHub Desktop.
Append parameters to POST in Intercept(OkHttp)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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