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

邮差的信 提交于 2019-12-18 14:54:32

问题


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.OnErrorNotImplementedException: method POST must have a request body.
            at rx.Observable$30.onError(Observable.java:7334)
            at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:154)
            at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111)
            at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:197)
            at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:173)
            at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at 
     Caused by: java.lang.IllegalArgumentException: method POST must have a request body.
            at com.squareup.okhttp.Request$Builder.method(Request.java:236)
            at retrofit.client.OkClient.createRequest(OkClient.java:59)
            at retrofit.client.OkClient.execute(OkClient.java:53)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)

trying to access a post api

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

actual api call

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

                        }
                    });
        });

回答1:


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.




回答2:


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, "");



回答3:


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.




回答4:


just include in @Post Method

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




回答5:


downgrade OkHttp to version 2.3.0




回答6:


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



来源:https://stackoverflow.com/questions/30358545/caused-by-retrofit-retrofiterror-method-post-must-have-a-request-body

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!