0

Is it possible to send a PATCH request with a complex object in the request body? The following works fine but it sends the object as url parameters not in the request body..

  //region - - - - - - - - - - - - - - - update with PATCH
        patch: function(url, obj, funcSuccess){

            // server call
            var resP =  resource(appConfigSvc.apiBaseUrl + url, obj, {
                'update': {
                    method:'PATCH',
                    headers: {'Content-Type': 'application/json' }
                }
            });
            var defer = q.defer();
            resP.update(
                function(data) {
                    defer.resolve(data);
                    if(funcSuccess){
                        funcSuccess(data);
                    }
                },
                function(response) {
                    //responseHandlerSvc.handleResponse(response);
                    defer.reject(response);
                });
            return defer.promise;
        },
        //endregion

WebApi doesn't have a problem accepting patch request body. Postman also allows sending patch requests with body. The only problem is Angular $resource.

Bucket
  • 7,415
  • 9
  • 35
  • 45
Ashraf Fayad
  • 1,433
  • 4
  • 17
  • 31

2 Answers2

1

Check this out. Although patch and put methods fail to send payload (on working with post)

Community
  • 1
  • 1
Murwa
  • 2,238
  • 1
  • 20
  • 21
  • Thanks. I have decided to stop using $resource. Now, I only use $http and it has no problem sending payloads with a patch request. – Ashraf Fayad Jul 23 '15 at 18:23
0

I created this method to send any request I need using $http. Nice and simple..

function send(method, url, obj, params){

    var deferred = $q.defer();
    var promise = http(
        {
            method:method,
            url: url,
            data:obj,
            headers:{'Content-Type':'application/json', 'Accept':'application/json'},
            params:params
        })
        .success(function(data) {
            deferred.resolve(data);
        })
        .error(function(msg, code) {
            deferred.reject(msg);
        });

    return deferred.promise;
}
Ashraf Fayad
  • 1,433
  • 4
  • 17
  • 31