我正在使用 Pyramid Cornice创建一个 API 供 Backbone.js 应用程序使用。我当前的代码对于 GET 和 POST 请求完美运行,但在收到 PUT 请求时返回 404 错误。我相信这是因为 Backbone 将它们作为 http://example.com/api/clients/ID
发送,其中 ID
是相关对象的 ID 号。
我的 Cornice 设置代码是:
clients = Service(name='clients', path='/api/clients', description="Clients")
@clients.get()
def get_clients(request):
...
@clients.post()
def create_client(request):
...
@clients.put()
def update_client(request):
...
Cornice 似乎只注册了路径 /api/clients
而不是 /api/clients/{id}
。我怎样才能让它匹配两者?
最佳答案
documentation给出了一个同时具有单独路径 (/users/{id}
) 和对象路径 (/users
) 的服务示例。这对你有用吗?
@resource(collection_path='/users', path='/users/{id}')
快速浏览一下code for the resource
decorator表明它主要创建了两个 Service
:一个用于对象,一个用于集合。您的问题可能可以通过添加另一个 Service
来解决:
client = Service(name='client', path='/api/clients/{id}', description="Client")
关于python - 当发送到 Pyramid/Cornice 应用程序时,Backbone.js HTTP PUT 请求失败并出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13026694/