pythonbackbone.jspyramidcornice

Backbone.js HTTP PUT requests fail with a 404 error when sent to a Pyramid/Cornice app


I'm using Pyramid with Cornice to create an API for a Backbone.js application to consume. My current code is working perfectly for GET and POST requests, but it is returning 404 errors when it receives PUT requests. I believe that this is because Backbone sends them as http://example.com/api/clients/ID, where ID is the id number of the object in question.

My Cornice setup code is:

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):
    ...

It seems that Cornice only registers the path /api/clients and not /api/clients/{id}. How can I make it match both?


Solution

  • The documentation gives an example of a service that has both an individual path (/users/{id}) and an object path (/users). Would this work for you ?

    @resource(collection_path='/users', path='/users/{id}')
    

    A quick glance at the code for the resource decorator shows that it mainly creates two Service : one for the object and one for the collection. Your problem can probably be solved by adding another Service :

    client = Service(name='client', path='/api/clients/{id}', description="Client")