【问题标题】:Performing PUT , PATCH in python eve application returns , The method is not allowed for the requested URL在python eve应用程序中执行PUT,PATCH返回,请求的URL不允许该方法
【发布时间】:2015-07-25 04:47:59
【问题描述】:

我有一个使用 Python Eve 编写的网络服务。我正在尝试对已创建的资源执行 PUT 请求。我在我的settings.py 文件中将item_methods 定义为PATCH,但是当我拨打电话时收到以下响应:-

{
  "_status": "ERR",
  "_error": {
      "message": "The method is not allowed for the requested URL.",
      "code": 405
  }
}

按照请求有效负载和我点击的 URL

网址:- http://127.0.0.1:5000/puburl/

请求有效负载:-

[
 {
  "puburl": "https://github.com/tushar",
  "userid": "xFGellL",
  "_etag": "714df986e0bf802962a6b8cb4b9b1513e1709d7b"
 }
]

我的settings.py 文件如下:-

__author__ = 'sappal'

# pulling DBSchema from DBTableSchema
from DBSchema.DBTableSchema import DBTableSchema
from Configs import Configs

dbtableSchema = DBTableSchema()


# Let's just use the local mongod instance. Edit as needed.
# Please note that MONGO_HOST and MONGO_PORT could very well be left
# out as they already default to a bare bones local 'mongod' instance.
## LOCALHOST ENTRIES
MONGO_HOST = Configs.MONGO_DB_HOST
MONGO_PORT = Configs.MONGO_DB_PORT
MONGO_USERNAME = Configs.MONGO_DB_USER_NAME
MONGO_PASSWORD = Configs.MONGO_DB_PASSWORD
MONGO_DBNAME = Configs.MONGO_DB

# Enable reads (GET), inserts (POST) and DELETE for resources/collections
# (if you omit this line, the API will default to ['GET'] and provide
# read-only access to the endpoint).
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']

# Enable reads (GET), edits (PATCH), replacements (PUT) and deletes of
# individual items  (defaults to read-only item access).
ITEM_METHODS = ['GET', 'PATCH', 'DELETE']

# Used for implementing user-resource restricted access.
# Returns the documents which are associated with particular user
AUTH_FIELD = 'userid'

people = {
  'item_title': 'person',
  'cache_control': 'max-age=10,must-revalidate',
  'cache_expires': 10,
  'resource_methods': ['GET', 'POST'],
  'schema': dbtableSchema.schema_people,
  'public_methods': ['POST']
}

org = {
  'item_title': 'org',
  'cache_control': 'max-age=10,must-revalidate',
  'cache_expires': 10,
  'resource_methods': ['GET', 'POST'],
  'schema': dbtableSchema.schema_people_org
}

puburl = {
  'item_title': 'puburl',
  'cache_control': 'max-age=10,must-revalidate',
  'cache_expires': 10,
  'resource_methods': ['GET', 'POST'],
  'item_methods': ['PATCH', 'PUT'],
  'schema': dbtableSchema.schema_people_pub_url
}

address = {
  'item_title': 'address',
  'cache_control': 'max-age=10,must-revalidate',
  'cache_expires': 10,
  'resource_methods': ['GET', 'POST'],
  'schema': dbtableSchema.schema_people_address
}

contactnumber = {
  'item_title': 'contactnumber',
  'cache_control': 'max-age=10,must-revalidate',
  'cache_expires': 10,
  'resource_methods': ['GET', 'POST'],
  'schema': dbtableSchema.schema_people_contact_number
}

template = {
  'item_title': 'template',
  'cache_control': 'max-age=10,must-revalidate',
  'cache_expires': 10,
  'resource_methods': ['GET', 'POST'],
  'schema': dbtableSchema.schema_template
}

usersharedcontacts = {
  'item_title': 'usersharedcontacts',
  'cache_control': 'max-age=10,must-revalidate',
  'cache_expires': 10,
  'resource_methods': ['GET', 'POST'],
  'schema': dbtableSchema.schema_people_with_user_shared_contacts
}

cardholder = {
  'item_title': 'cardholder',
  'cache_control': 'max-age=10,must-revalidate',
  'cache_expires': 10,
  'resource_methods': ['GET', 'POST'],
  'schema': dbtableSchema.schema_people_card_holder
}

DOMAIN = {
  'people': people,
  'org': org,
  'puburl': puburl,
  'address': address,
  'contactnumber': contactnumber,
  'template': template,
  'usersharedcontacts': usersharedcontacts,
  'cardholder': cardholder
}

关于呼叫失败的任何想法..

【问题讨论】:

    标签: python web-services python-2.7 flask eve


    【解决方案1】:

    乍一看,我会说您访问的 URL (http://127.0.0.1:5000/puburl/) 看起来像一个资源(集合)端点,而 PUT 是一个文档(项目)端点。这就是您将其添加到item_methods 而不是resource_methods 的原因。您想点击 http://127.0.0.1:5000/puburl/<id> 之类的东西,以便 PUT 工作。

    【讨论】:

    • 感谢@nicola,我正在为 MongoDB 中的文档注入我的自定义 ID,所以我无法修补我的项目,我已经对我的代码进行了更改,现在我可以做一个PATCH http://127.0.0.1:5000/puburl/<id>。感谢您的帮助
    【解决方案2】:

    我认为问题在于 etag 必须位于带有子句 'If-Match' = etag 的标题中。

    您可以在http://python-eve.org/features.html查看更多关于“数据完整性和并发控制”的信息

    【讨论】:

    • 他会得到一个403 是这样的。
    猜你喜欢
    • 2021-10-16
    • 2015-06-18
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 2017-01-10
    • 2020-07-30
    • 2012-04-11
    • 2014-01-08
    相关资源
    最近更新 更多