Caused by: retrofit.RetrofitError: method POST must have a request body

后端 未结 6 1655
悲&欢浪女
悲&欢浪女 2021-01-02 01:12

I am using retrofit to make a post api call, I am getting the following error while trying to hit the endpoint.

     Caused by: rx.exceptions.OnErrorNotImple         


        
6条回答
  •  难免孤独
    2021-01-02 02:02

    I solved this issue by replacing @Query to @Field, here is how:

    Not working code:

    @POST("/my/url/path")
    Result postToServer(
            @Query("user_name") String userName);
    

    Working example:

    @FormUrlEncoded
    @POST("/my/url/path")
    Result postToServer(
            @Field("user_name") String userName);
    

    For methods that do not have any fields I had to add empty string like below

    Result postToServer(@Path("my_path") String myPath,
                                   @Body String emptyString);
    

    And call it passing "":

    restClient.postToServer(myPath, "");
    

提交回复
热议问题