Skip to content

Instantly share code, notes, and snippets.

@mrhether
Last active December 23, 2022 17:21
Show Gist options
  • Save mrhether/6f497ad7c669722f2a6b174df91233fd to your computer and use it in GitHub Desktop.
Save mrhether/6f497ad7c669722f2a6b174df91233fd to your computer and use it in GitHub Desktop.
XHttpMethodOverride Interceptor for OkHttp and Retrofit.
public OkHttpClient getOkClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new XHttpMethodOverrideInterceptor());
return builder.build()
}
public class XHttpMethodOverrideInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String method = request.method();
if (method.equalsIgnoreCase("PUT") || method.equalsIgnoreCase("DELETE")) {
request = request.newBuilder()
.post(request.body())
.addHeader("x-http-method-override", method).build();
}
return chain.proceed(request);
}
}
@marcosdxt
Copy link

marcosdxt commented Jul 30, 2021

Nice codem,man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment