Search code examples
javaandroidpostrx-java2

RXJava Retrofit returns POST HTTP Error 500


I get a HTTP 500 INTERNAL SERVER ERROR on a POST using RXJAVA and RETROFIT and I dont fully understand how this Call works, but the other Call is working fine, with the same BASE_URL constant.

Here is my interface:

public interface AuthApi {

    @GET("user/{id}")  //users/id
    Flowable<User> getUser(
            @Path("id") int id
    );

    @POST("login")
    @FormUrlEncoded
    Flowable<User> login(
            @Field("username") String username,
            @Field("password") String password
    );
}

The @GET Method works fine The @POST Method returns an error

I believe this has something to do, with how the string or post request is getting structured, because Postman is working perfectly with following json:

{
    "username": "Test1",
    "password": "test1"
}

Here is the the rxjava call:

authApi.login("Test1","test1")
                .toObservable()
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<User>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(User user) {
                        Log.d(TAG,"onNext :"+ user.getEmail());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: ", e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

This returns the HTTP 500 INTERNAL SERVER ERROR

But to give you guys more details, here is the error log:

E/AuthViewModel: onError: 
    retrofit2.adapter.rxjava2.HttpException: HTTP 500 INTERNAL SERVER ERROR
        at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:54)
        at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:37)
        at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:47)
        at io.reactivex.Observable.subscribe(Observable.java:10838)
        at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
        at io.reactivex.Observable.subscribe(Observable.java:10838)
        at io.reactivex.internal.operators.flowable.FlowableFromObservable.subscribeActual(FlowableFromObservable.java:29)
        at io.reactivex.Flowable.subscribe(Flowable.java:12978)
        at io.reactivex.internal.operators.flowable.FlowableOnBackpressureLatest.subscribeActual(FlowableOnBackpressureLatest.java:32)
        at io.reactivex.Flowable.subscribe(Flowable.java:12978)
        at io.reactivex.Flowable.subscribe(Flowable.java:12924)
        at io.reactivex.internal.operators.observable.ObservableFromPublisher.subscribeActual(ObservableFromPublisher.java:31)
        at io.reactivex.Observable.subscribe(Observable.java:10838)
        at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
        at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
        at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
        at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

This is the expected response:

{
    "email": "test1m@test1.ie",
    "id": 11,
    "username": "Test1"
}

Do I overlook something? do you guys have any tips for me? And is there a way to debug the POSt request to actualy see the POSt request?


Solution

  • Check this answer. This is how you can check network request and responses for your app. Also if you are sending POST request with @FormUrlEncoded, that option needs to be enabled on server side to be processed right way. You can try with @Body instead of @FormUrlEncoded and @Field.

    @POST("login")
    Flowable<User> login(
         @Body LoginData data
    );
    
    

    Where UserInfo is class.

    
    public class LoginData {
    
        private String username;
        private String password;
    
        public LoginData(String username, String password) {
            this.username = username;
            this.password = password;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
    }