So I want to make a PATCH request using Retrofit but currently I cannot add okhttp to my classpath. When I attempt to make a PATCH request I get the stack trace below. Is there any other way I could use Patch without using okhttp?
java.net.ProtocolException
at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:644)
at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:50)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:358)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:264)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:315)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:1019)
06-09 10:53:09.349 1809-1897/**.****.****** D/Retrofit﹕ ---- END ERROR
This is a limitation of
HttpUrlConnection
. You can either use Apache or OkHttp which both supportPATCH
as an alternative client. This can be done explicitly in the builder:Additionally, some servers allow specifying an
X-HTTP-Method-Override
header for changing the method. With this you would send aPOST
but includePATCH
as this header value. Again, this requires server support.Finally, one other option would be to subclass Retrofit's
UrlConnectionClient
and use reflection to change the field which holds the HTTP method. This is very fragile, prone to future breakage, and is the worst option in my opinion.