sandy
sandy

Reputation: 963

@DELETE method is not supporting(Non-body HTTP method cannot contain @Body or @TypedOutput.)

@DELETE("/job/deletejob")
 Observable<JobDeleteResponseModel> jobDelete( @Body JobDeleteRequestModel model);

am getting this error:

Non-body HTTP method cannot contain @Body or @TypedOutput

Upvotes: 96

Views: 50941

Answers (9)

Nikolay
Nikolay

Reputation: 39

Kotlin with suspend function. By default DELETE method do not supports body. You should explicitly enable it.

@HTTP(
    method = "DELETE",
    path = "path/to/api/{someRoute}/{id}", 
    hasBody = true
)
suspend fun delete(
    @Path("someRoute") someRoute: String,
    @Path("id") id: String,
    @Body body: SomeBodyModel,
): Response<Unit>

Use it something like this

suspend fun delete(model: SomeBodyModel) {
    val response = api.delete(model)
    if (!response.isSuccessful) throw HttpException(response)
}

Upvotes: 2

Navin Kumar
Navin Kumar

Reputation: 4027

Retrofit 2, i have change the below code from this

@DELETE("example/user/{id}/list")
suspend fun deleteUserList(@Path(value = "id", encoded = false)key: Int, @Body request: DeleteUserListRequest): Response<BaseResponse>

to

@HTTP(method = "DELETE", path = "example/user/{id}/list",hasBody = true)
suspend fun deleteUserList(@Path(value = "id", encoded = false)key: Int, @Body request: DeleteUserListRequest): Response<BaseResponse>

Above code is working for me

Upvotes: 2

Yogesh Bansal
Yogesh Bansal

Reputation: 11

@HTTP(method = "DELETE", path = "{urlPath}", hasBody = true)

Also this is working fine. It happens because the request contains a body but we have not defined that yet.

Upvotes: 1

Aditya Patil
Aditya Patil

Reputation: 1496

You need to specify parameters
method, path, hasBody

Kotlin way

@HTTP(method = "DELETE", path = "event/eventRemovePicture", hasBody = true)
fun callDeleteImage(
    @Body body: RequestBody
): Call<RemoveEventPictureResponse>

Upvotes: 26

Junior Carrillo
Junior Carrillo

Reputation: 81

Change

@DELETE("/job/deletejob")
Observable<JobDeleteResponseModel> jobDelete( @Body JobDeleteRequestModel model);

to

@HTTP(method = "DELETE", path = "/job/deletejob", hasBody = true)
Observable<JobDeleteResponseModel> jobDelete( @Body JobDeleteRequestModel model);

The difference is in

@DELETE("/job/deletejob") // For DELETE without body
@HTTP(method = "DELETE", path = "/job/deletejob", hasBody = true) // For DELETE with body

Upvotes: 8

Ramakrishna Joshi
Ramakrishna Joshi

Reputation: 1578

Kotlin Code :

path is not required if your first argument to interface method is a url annotated with @Url Example :

@HTTP(method = "DELETE", hasBody = true)
fun deleteStudentFromDatabase(
    @Url url: String,
    @Body payload: StudentModel
 ): Observable<Any>

If first argument to interface method is not a url then use this

    @HTTP(method = "DELETE", path = "{urlPath}", hasBody = true)
    fun deleteStudentFromDatabase(
        @Body payload: StudentModel,
        @Path("urlPath") url: String
     ): Observable<Any>

Upvotes: 5

Makarand
Makarand

Reputation: 1123

I had similar error.

In my case I was using @GET in Interface but then I corrected it to @POST method and it worked.

Upvotes: 16

Shiv Kumar
Shiv Kumar

Reputation: 675

try this it's work

@HTTP(method = "DELETE", path = "api/v3/delete", hasBody = true)
Call<ResponseBody> RESPONSE_BODY_CALL(@Header("Authorization") String authorization, @Body HashMap<String, List> stringListHashMap);

or check https://github.com/square/retrofit/issues/974

Upvotes: 14

AndroidEx
AndroidEx

Reputation: 15824

I've used this official workaround recently:

@HTTP(method = "DELETE", path = "/job/deletejob", hasBody = true)
Observable<JobDeleteResponseModel> jobDelete(@Body JobDeleteRequestModel model);

Upvotes: 256

Related Questions