改造-http 405 响应?
Posted
技术标签:
【中文标题】改造-http 405 响应?【英文标题】:Retrofit-http 405 response? 【发布时间】:2017-05-21 22:33:44 【问题描述】:我正在尝试创建一个登录页面,并且我使用 django 作为后端。使用的身份验证是基于令牌的。我试图发布用户名和密码,这给了我一个 405 http 代码错误,这不是我预期的情况。而且它还说它不期望我做的 GET 方法。它在https://www.hurl.it/ 中以正确的方式工作。我在此处发布了图像和代码。请帮忙!
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);
拦截器日志是
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)
后端是 Django,我正在使用基于令牌的身份验证。它在 hurl.it 中完美运行。
这是原木。
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)
【问题讨论】:
我使用 okhttp 发送请求,它工作正常。谁能解决这个问题? 【参考方案1】:我在我的程序中发现了错误。实际上这是我的 Url 提供给 Restprovider 类的问题。它应该是
"/api-token-auth/
"
只是一个斜线就完全不同了。感谢所有尝试过的人。
【讨论】:
以上是关于改造-http 405 响应?的主要内容,如果未能解决你的问题,请参考以下文章
CORS 问题? - 预检响应包含无效的 HTTP 状态代码 405