简体   繁体   中英

request.data is empty in django REST API Request for PATCH request from Safari using AngularJS

I send a patch request via web browser (chrome/safari) to Django Rest framework. When I tried to access data request.data values from Django backend when the API ( https://test.mysite.com/cart/ ) is triggered via Google Chrome, I can read the data. But when the same PATCH request triggered from safari browser does not give me the request.data (show as empty)

JS to call the cart API:

    var order = {};
    order.num_cases = 8;

    var absloutePatchURL = '/cart/'+order.cartID;
    window.console.log('Update cart absolute URL:',absloutePatchURL);
    $http.patch(absloutePatchURL,order, {data:JSON}). success(function(data, status, headers, config) {
        window.console.log("Your cart is updated successfully");                
      }).error(function(data, status, headers, config) {
        toastr.error('Sorry, unable to update your item to the cart. Please try later.');
    });

Request Details:

URL: https://test.mysite.com/cart/
Request Type: PATCH
Request Data: {"num_cases":6}   

While accessing data requested via PDB on the request object on Django.

Django Code to Access:

class CartViewSet(CartMixin, ModelViewSet):

    def update(self, request, *args, **kwargs):

        import pdb
        pdb.set_trace()

        logger.debug("Request Data: {0}".format(request.data))

        return ModelViewSet.update(self, request, *args, **kwargs)

Django console log when requested from Chrome:

-> return ModelViewSet.update(self, request, *args, **kwargs)
(Pdb) 
(Pdb) request.data
<QueryDict: {u'{"num_cases":8}': [u'']}>

Django console log when requested from SAFARI:

<QueryDict: {}>
(Pdb) request.data
<QueryDict: {}>

Make sure you are setting the content type as application/json . It looks as if rest framework is treating it as application/x-www-form-urlencoded .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM