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

后端 未结 6 1630
悲&欢浪女
悲&欢浪女 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 01:40

    downgrade OkHttp to version 2.3.0

    0 讨论(0)
  • 2021-01-02 01:44

    @Body String emptyBody wasn't working for me as backend server I was working with, was returning Bad request and not accepting empty string as a body.

    I've just sent empty JSON instead of empty String

    @POST("/some/url) Observable<Thing> doThing(@Body JsonElement empty);

    and then I just called my method like this:

    doThing(new JsonObject());

    Hope it will help if someone has similar problem.

    0 讨论(0)
  • 2021-01-02 01:47

    It looks like Retrofit wants POST requests to have a payload. There's already an issue for it: https://github.com/square/retrofit/issues/854

    As a workaround, you could do something like this:

    @POST("/service/v2/auth/ip-address")
    rx.Observable<AuthState> verifyIP(@Body Object dummy);
    

    and then do:

    LoginService service = CKRestClient.get().create(LoginService.class);
    
    service.verifyIP(null).observeOn(AndroidSchedulers.mainThread()).subscribe(
      new Action1<AuthState>() {
        @Override
        public void call(AuthState authState) {
          // ...
        }
      });
    });
    

    Or, if service.verifyIP(null) throws a NPE, replace it with service.verifyIP("") or similar.

    0 讨论(0)
  • 2021-01-02 01:48

    It seems that Retrofit rel.1.9 is prone by the issue (https://github.com/square/retrofit/issues/854).

    In the meantime, consider downgrading to rel.1.6 until current release will be fixed. It is verified that rel.1.6 is free of this particular bug.

    0 讨论(0)
  • 2021-01-02 01:56

    just include in @Post Method

    @Body String dummyValue , if body not required you must write this query and send "" to server

    0 讨论(0)
  • 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, "");
    
    0 讨论(0)
提交回复
热议问题