Retrofit: Custom Delete Method with Retrofit 2.0

Created on 30 Sep 2015  路  8Comments  路  Source: square/retrofit

Accoring to @JakeWharton it is possible to overwrite Rest Methods. I need a customized method (DELETE with Body) as it isn麓t allowed by retrofit even if it麓s allowed by rfc spec.
But this does not work anyore since the Annotation @RestMethod was removed since retrofit 2.0
So how can I send now a HTTP-DELETE with Body?

see alsow: https://github.com/square/retrofit/issues/426
and https://github.com/square/okhttp/issues/605

If I use default mechnism of retrofit 2.0 i get a stacktrace

@DELETE("/myurl")
    Call<String> deactivateUserPermanently(
            @Header("Authorization") String systemAuthLine,
            @Body MyBodyRequest myBodyRequest
    );
java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body.
    for method MyProxy.deactivateUserPermanently
    at retrofit.Utils.methodError(Utils.java:177)
    at retrofit.Utils.methodError(Utils.java:167)
    at retrofit.RequestFactoryParser.parseParameters(RequestFactoryParser.java:359)
    at retrofit.RequestFactoryParser.parse(RequestFactoryParser.java:60)
    at retrofit.MethodHandler.create(MethodHandler.java:30)
    at retrofit.Retrofit.loadMethodHandler(Retrofit.java:151)
    at retrofit.Retrofit$1.invoke(Retrofit.java:132)
    at com.sun.proxy.$Proxy7.deactivateUserPermanently(Unknown Source)

Most helpful comment

Actually, the correct syntax is:
@HTTP(method = "DELETE", path = "/foo", hasBody = true)

All 8 comments

You can use the @HttpMethod annotation to create custom verbs in Retrofit 2.

Copied from the referred issue, as @HttpMethod is misleading.

interface Example {
  @HTTP(method = "CUSTOM1", path = "/foo")
  Call<ResponseBody> method();
}

@Zhuinden This is correct one.
interface Example {
@HTTP(method = "DELETE", path = "/foo, hasBody = true")
Call method();
}

Actually, the correct syntax is:
@HTTP(method = "DELETE", path = "/foo", hasBody = true)

Hey folks!
What if need to send query params and body? I've tried @http(method = "DELETE", path = "/foo", hasBody = true) but retrofit (or OkHttp) is adding / to the end of path, so, i'm getting something like this: /path/to/url/?param1=abc. Because of this, i'm getting a 404 from backend :(

Thanks in advance.

Hey guys,

If you have conflict with the serialization of the answer, this works for me

API

 @HTTP(method = "DELETE", path ="----", hasBody = true)
    fun deleteGiftCard(
                       @Body body: ObjectRequest): Observable<okhttp3.ResponseBody>

Response

compositeDisposable.add( observable.subscribeOn (Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        { result ->

                           val response = Gson().fromJson(result.string(), YourObjectResponse::class.java)

                            (response (result != null,response))
                        },
                        {
                            (response (false,0))
                        }
                )

@Santi92 out of curiousity, why not use GsonConverterFactory and receive Observable<YourObjectResponse> from Retrofit?

@Zhuinden I got it.
But do not serialize it, in the okHttpClient-Interceptor interceptor if you show the body.
Super strange, the names of the keys is fine.

Does it work for you?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vkislicins picture vkislicins  路  3Comments

jpshelley picture jpshelley  路  4Comments

chriskessel picture chriskessel  路  3Comments

ramonmoraes8080 picture ramonmoraes8080  路  3Comments

Hesamedin picture Hesamedin  路  3Comments