Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-tastypie PATCH gives me a "400 (Bad Request)"

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

like image 749
Christopher Penkin Avatar asked Dec 16 '25 22:12

Christopher Penkin


1 Answers

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 Places

Some 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 an X-HTTP-Method-Override header. For example, to send a PATCH request over POST, 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.

like image 72
Tadeck Avatar answered Dec 19 '25 18:12

Tadeck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!