I\'m trying to make an asynchronous POST and DELETE which is form url encoded using Retrofit in Android 4.4
Here is my client -
@FormUrlEncoded
@POS
Updated answer for Retrofit 2.0:
Retrofit 2 doesn't seem to have @RestMethod any more, so here is what works:
@FormUrlEncoded
@HTTP(method = "DELETE", path = INetwork.API_BASE_PREFIX + "/memberships.json", hasBody = true)
void leave(@Field("id") String id, Callback<?> cb);
For retrofit 2.+
@FormUrlEncoded
@HTTP(method = "DELETE", path = INetwork.API_BASE_PREFIX + "/memberships.json", hasBody = true)
Callback<?> cb(@Field("id") String id);
and for RxRetrofit 2.+
@FormUrlEncoded
@HTTP(method = "DELETE", path = INetwork.API_BASE_PREFIX + "/memberships.json", hasBody = true)
Observable<?> cb(@Field("id") String id);
The RFC for HTTP is unclear on whether or not the DELETE method is allowed to have a request body or not. Retrofit is raising an error on the side of caution by not having one.
However, you can still include one (assuming the HTTP client supports it) by using a custom HTTP method annotation.
package com.myapp;
@Target(METHOD)
@Retention(RUNTIME)
@RestMethod(value = "DELETE", hasBody = true)
public @interface BODY_DELETE {
String value();
}
Now specify your interface method using the custom annotation that you defined.
@FormUrlEncoded
@BODY_DELETE(INetwork.API_BASE_PREFIX + "/memberships.json")
void leave(@Field("id") String id, Callback<?> cb);