Retrofit throwing IllegalArgumentException exception for asynchronous FormUrlEncoded DELETE call

前端 未结 2 1098
星月不相逢
星月不相逢 2020-12-16 16:56

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         


        
2条回答
  •  天涯浪人
    2020-12-16 17:27

    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);
    

提交回复
热议问题