Retrofit-http 405 response?

4.8k views Asked by At

I am trying to create a login page and i am using the django as backend. authentication used is token-based. i have tried to POST the username and password which gives me a 405 http code error which is not the case i expected. and also it says that it is not expecting GET method which i dint do. it works the right way in https://www.hurl.it/ .i have posted the images and code here. Please help !

LoginPresenter.java

subscription = RxUtil.io(restProvider.authenticate(userId, password))
                .subscribe(new Subscriber<JsonObject>() {
                    @Override
                    public void onCompleted() {
                        getMvpView().showProgress(false);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Timber.e(e);
                        getMvpView().showProgress(false);
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        Timber.d(jsonObject.toString());
                    }
                });

RestProvider.java

public interface RestProvider {

    @FormUrlEncoded
    @POST("/api-token-auth")
    Observable<JsonObject> authenticate(@Field("username") String user, @Field("password") String pass);

}

The interceptor log is

POST http://mylink.com/mypath http/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 52
username=myusername&password=mypassword
--> END POST (52-byte body)
<-- 405 Method Not Allowed http://mylink.com/mypath (84ms)
Date: Sat, 07 Jan 2017 04:36:48 GMT
Server: Apache/2.4.18 (Ubuntu)
Vary: Cookie
X-Frame-Options: SAMEORIGIN
Allow: POST, OPTIONS
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json
{"detail":"Method \"GET\" not allowed."}
<-- END HTTP (40-byte body)

The backend is Django and i am using Token based authentication. It works in hurl.it perfectly.

request in hurl

response in hurl

This is the timber log.

retrofit2.adapter.rxjava.HttpException: HTTP 405 Method Not Allowed
                                                                        at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:43)
                                                                        at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:38)
                                                                        at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:173)
                                                                        at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
                                                                        at rx.Subscriber.setProducer(Subscriber.java:211)
                                                                        at rx.internal.operators.OperatorSubscribeOn$1$1.setProducer(OperatorSubscribeOn.java:76)
                                                                        at rx.Subscriber.setProducer(Subscriber.java:205)
                                                                        at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
                                                                        at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
                                                                        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
                                                                        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
                                                                        at rx.Observable.unsafeSubscribe(Observable.java:10142)
                                                                        at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
                                                                        at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
                                                                        at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                                                                        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                        at java.lang.Thread.run(Thread.java:761)
1

There are 1 answers

0
jknair0 On BEST ANSWER

I have found the bug in my program. Actually this was the problem of my Url provided to the Restprovider class . It was supposed to be "/api-token-auth/" just a slash made all the difference. Thank you for all those who tried.