405 不允许 POST 方法 [英] 405 POST method not allowed

查看:90
本文介绍了405 不允许 POST 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OAuth 开发 REST 提供程序.我正在使用 Django RESTFramework 和 DjangoOAuthToolkit.我做了一个 GET,它工作得很好,但我正在尝试使用 POST,服务器响应 {"detail": "Method 'POST' not allowed."}这是我的代码:

I'm trying to develop a REST provider with OAuth. I'm using Django RESTFramework and DjangoOAuthToolkit. I did a GET and it works perfectly but I'm trying to use a POST and the server responds with {"detail": "Method 'POST' not allowed."} This is my code:

# views.py
@api_view(['POST'])
def pruebapost(request):
    usuario = User()
    access_token = Token.objects.get(
        key=request.POST['oauth_token']
    )
    usuario = access_token.user
    content = {'saludo': usuario.username}
    return Response(content)

# settings.py
OAUTH_AUTHORIZE_VIEW = 'principal.views.oauth_authorize'
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'
REST_FRAMEWORK = {
   'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    ),
   'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',
    ),
   'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.OAuthAuthentication',
    ),
}

我将其用作测试"客户端:

And I'm using this as a "test" client:

import urlparse
import oauth2 as oauth
import requests

consumer_key = "clave"
consumer_secret = "secreto"
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
resource_url = 'http://blablabla.pythonanywhere.com/prueba'
consumer = oauth.Consumer(key='clave', secret='secreto')
token = oauth.Token(key='e7456187a43141af8d2e0d8fa99b95b9',
                    secret='3wRIKoacff16tcew')

oauth_request = oauth.Request.from_consumer_and_token(
    consumer,
    token,
    http_method='POST',
    http_url=resource_url,
    parameters={'hola':'pepe'}
)
oauth_request.sign_request(
    oauth.SignatureMethod_HMAC_SHA1(),
    consumer,
    token
)
url = oauth_request.to_url()
response = requests.post(url, oauth_request.to_postdata())
print response.content

我不明白 REST Framework 文档对 405 Method not allowed 的说法

I don't understand what REST Framework documentation says about 405 Method not allowed

在发生未映射到视图上的处理程序方法的传入请求时引发."

"Raised when an incoming request occurs that does not map to a handler method on the view."

谢谢

推荐答案

user2663554

问题解决了,我错过了网址上的一个斜线.

Problem solved, I miss one slash on the url.

此响应代码 (405) 可能来自任意数量的问题,但最终通常是您使用了错误的 URL(如本例),或者您使用了错误的请求方法.有时两者兼而有之!

This response code (405) can come from any number of issues, but it generally ends up that either you are using the wrong URL (as in this case), or you are using the wrong request method. Sometimes it's both!

我经常看到人们在尝试更新单个资源 (/api/res/1) 时遇到此问题,但他们使用的是列表 url (/api/res) 不允许发出请求.这也可能反过来发生,即有人试图创建一个新实例,但他们正在向单个对象发送 POST 请求.

Quite often I see people getting this issue when they are trying to update an individual resource (/api/res/1), but they are using the list url (/api/res) which doesn't allow the request to be made. This can also happen in the reverse, where someone is trying to create a new instance, but they are sending a POST request to the individual object.

在某些情况下,使用了错误的 url,因此用户请求标准的非 API 视图并认为它是 API 视图(/res 而不是 /api/res).因此,请务必始终检查您的网址!

In some cases, the wrong url is being used, so users are requesting a standard non-API view and thinking it is an API view (/res instead of /api/res). So make sure to always check your urls!

这篇关于405 不允许 POST 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆