问题
I am running a Django site on Apache which is front'ed by Nginx instance to serve my static media.
I expose an API via django-tastypie to a model that I need to PATCH a field on. When I do local testing (via the django runserver) everything works as expected. On the live server however I get "400 (Bad Request)" returned.
I've read a few places saying that Nginx does not support PATCH? Is that right? Is there a good workaround for this? Am I doing something wrong?
I only send through the fields I want to update via the postData.
JQuery Code:
$.ajax({url: '...',
type: 'PATCH',
accepts: 'application/json',
contentType: 'application/json',
dataType: 'json',
data: postData,
processData: false,
success: function() {
// Success Code!
},
error: function() {
// Error Code!
}
});
Tastypie Resource:
class ReceivedMessageResource(ModelResource):
"""
"""
campaign = fields.ForeignKey(CampaignResource, 'campaign')
campaign_name = fields.CharField(readonly=True)
campaign_id = fields.IntegerField(readonly=True)
message_type = fields.CharField(readonly=True)
display_date = fields.CharField(readonly=True)
attachments = fields.ToManyField('apps.campaign.api.AttachmentResource',
'attachment_set',
related_name='message',
full=True)
class Meta:
queryset = ReceivedMessage.objects.all()
resource_name = 'message'
filtering = {'id': ALL,
'campaign': ALL_WITH_RELATIONS}
excludes = ['reason', 'provider', 'loyalty_profile', 'original_message', 'date_received']
allowed_methods = ['get', 'post', 'put', 'delete', 'patch']
paginator_class = ReceivedMessagesPaginator
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
Any direction on how to sort this will be appreciated :)
回答1:
If you are using the latest version of TastyPie (the one from GitHub repository, since August 5th), you can follow the instructions from the documentation:
Using
PUT/DELETE/PATCH
In Unsupported PlacesSome places, like in certain browsers or hosts, don’t allow the
PUT
/DELETE
/PATCH
methods. In these environments, you can simulate those kinds of requests by providing anX-HTTP-Method-Override
header. For example, to send aPATCH
request overPOST
, you’d send a request like:
curl --dump-header - -H "Content-Type: application/json" -H "X-HTTP-Method-Override: PATCH" -X POST --data '{"title": "I Visited Grandma Today"}' http://localhost:8000/api/v1/entry/1/
So if your host does not support this method, add X-HTTP-Method-Override
header with the name of the method you are trying to perform.
回答2:
If PATCH isn't getting past your HTTP server, you could fake it. Use a POST request, and add the header 'X-HTTP-Method-Override': 'PATCH'. This is supported in the master branch of Tastypie at the time of this posting.
If you are using an older version, such as the current stable release 0.9.11, you may need a small patch. Something like this gist will teach Tastypie to use that header.
The relevant piece is here:
if request_method == 'post' and 'HTTP_X_HTTP_METHOD_OVERRIDE' in request.META:
request_method = request.META['HTTP_X_HTTP_METHOD_OVERRIDE'].lower()
来源:https://stackoverflow.com/questions/11868908/django-tastypie-patch-gives-me-a-400-bad-request