@DELETE方法不支持(非主体HTTP方法不能包含@Body或@TypedOutput.)

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

我遇到了这个错误:

非请求体HTTP方法不能包含@Body或@TypedOutput


尝试使用 Query 而不是 Body,因为 DELETE 请求中的 Bodies 没有定义的语义。请注意,在 DELETE 请求中发送 Body 可能会导致某些现有实现拒绝该请求。 - PN10
请查看此链接,它可能会对您有所帮助:https://github.com/square/retrofit/issues/458 - PN10
9个回答

252

我最近使用了这个官方的解决方法:

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

1
不起作用。我已经在下面的链接中使用了: https://stackoverflow.com/questions/48539478/custom-http-method-not-working-in-retrofit-2#你能帮忙吗? - Chetak Bhimani
一切都很顺利。我被困在整个一天中,准备从后端改变我的API方法,但你拯救了我的一天。谢谢。 - Ajay Mistry
如果您在接口方法的第一个参数是用 @Url 注释的 url,则 path 不是必需的。更多细节请参见此处:stackoverflow.com/a/62920127/8956093 - Ramakrishna Joshi

23

您需要指定参数
方法、路径、是否有请求体

Kotlin方式

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

1
谢谢,它运作正常。 - Mahbubur Rahman Khan

14

我遇到了类似的错误。

我的情况是在接口中使用了 @GET ,但我将其更正为 @POST 方法后,问题得以解决。


谢谢,我也遇到了同样的问题,因为我复制粘贴了方法。 - Pravin Desai
我也不小心这样做了。但是,使用 @Body 动词进行 GET 请求是没有意义的。 - dipakbari4

12

试试这个,它有效

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

或者查看https://github.com/square/retrofit/issues/974


8

更改

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

to

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

区别在于

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

5

Kotlin 代码:

如果你的接口方法的第一个参数带有 @Url 注解,则不需要指定 path。例如:

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

如果接口方法的第一个参数不是 URL,则使用此参数。
    @HTTP(method = "DELETE", path = "{urlPath}", hasBody = true)
    fun deleteStudentFromDatabase(
        @Body payload: StudentModel,
        @Path("urlPath") url: String
     ): Observable<Any>

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

这也可以正常工作。这是因为请求包含一个主体,但我们尚未定义它。


1

Retrofit 2,我已经将以下代码从这里更改

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

@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>

上述代码对我来说是有效的


1

Kotlin中的挂起函数。 默认情况下,DELETE方法不支持请求体。您应该显式启用它。

@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>

使用方法如下

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接