How to send HTTP delete with body in modification?

When I try to create a delete method:

public interface ImageService { @DELETE("api/v1/attachment") Call<BaseResponse> delete(@Body DeleteModel deleteModel); } 

I get an error that basically boils down to these lines from stacktrace:

 E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Failure delivering result java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body. Caused by: java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body. 

How to add a body to a removal method?

I searched here, but found 3 answers and used nothing to modify.

+8
source share
5 answers

More simplified answer.

 @FormUrlEncoded @HTTP(method = "DELETE", path = "/api/analysis_delete", hasBody = true) Call<Analysis_Delete_RequestResult_Api10> analysis_delete_api10(@Field("seq") String seq); 

This will do the trick.

+32
source

Here is my version

 @HTTP(method = "DELETE", path = "{login}", hasBody = true) Call<ResponseBody> getData(@Path("login") String postfix, @Body Map<String, Object> options); 
+4
source

If you are using an older version that does not support @HTTP, you can also add another interface that implements @RestMethod

 import java.lang.annotation.Retention; import java.lang.annotation.Target; import retrofit.http.RestMethod; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** Make a DELETE request to a REST path relative to base URL with body. */ @Target(METHOD) @Retention(RUNTIME) @RestMethod(value = "DELETE", hasBody = true) public @interface DELETEWITHBODY { String value(); } 

And then usage becomes:

 public interface ImageService { @DELETEWITHBODY("api/v1/attachment") Call<BaseResponse> delete(@Body DeleteModel deleteModel); } 
+2
source

Here's an excerpt from the docs, this is a documented HTTP annotation feature.

 This annotation can also used for sending DELETE with a request body: interface Service { @HTTP(method = "DELETE", path = "remove/", hasBody = true) Call<ResponseBody> deleteObject(@Body RequestBody object); } 

https://square.imtqy.com/retrofit/2.x/retrofit/retrofit2/http/HTTP.html

+1
source

service:

 public interface ImageService { @Post("api/v1/attachment") Call<BaseResponse> delete(@Body DeleteModel deleteModel); } 

and in servercontroller

 import okhttp3.Request; private final class ApiInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request oldRequest = chain.request(); Request.Builder builder = oldRequest.newBuilder(); if(condition) { return chain.proceed(builder.build().newBuilder().delete(builder.build().body()).build()); } return chain.proceed(builder.build()); } } 

you need to call a condition, through something and you may have to do some filtering for url / header / body in order to remove the trigger,

if the header delete url / body / is not unique, so as not to interfere with the message or receive requests.

0
source

Source: https://habr.com/ru/post/1013828/


All Articles