Last active
December 23, 2022 17:21
-
-
Save mrhether/6f497ad7c669722f2a6b174df91233fd to your computer and use it in GitHub Desktop.
XHttpMethodOverride Interceptor for OkHttp and Retrofit.
This file contains 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
public OkHttpClient getOkClient() { | |
OkHttpClient.Builder builder = new OkHttpClient.Builder(); | |
builder.addInterceptor(new XHttpMethodOverrideInterceptor()); | |
return builder.build() | |
} |
This file contains 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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice codem,man!