0

I am trying to update data through my API using http Patch method. But i am getting a Bad Response or Internal Server Error.

Here is my JSON call:

$http.patch(baseUrl + '/users/' + currentUserEmail,data).success(success).error(error)
1
  • My API PATCH Method needs an IF-Match header but isn't it added automatically? Commented Oct 28, 2015 at 8:29

1 Answer 1

2

You can add needed headers using the optional 3rd params of $http.patch :

var config = {headers: {'IF-Match': 'your-data'}};
$http.patch(baseUrl + '/users/' + currentUserEmail,data, config).success(success).error(error)

The documentation provides info about custom config options.

If you wish to add custom headers to every request automatically you can use the $http interceptor :

angular.module('app').factory('HttpInterceptor', function () {
  return {
    request: function (config) {
      if (config.method === 'PATCH')
         config.headers['IF-Match'] = 'your-data';
      return config;
    }
  };
});

angular.module('app').config(['$httpProvider', '$resourceProvider', function ($httpProvider, $resourceProvider) {
    // Add the interceptor to the $httpProvider to intercept http calls
    $httpProvider.interceptors.push('HttpInterceptor');
}])

EDIT: to answer your comment about how to get info from GET request. In the http interceptor, you can intercept response as well :

angular.module('app').factory('HttpInterceptor', function () {
 var etag = null;
  return {
    request: function (config) {
      if (config.method === 'PATCH')
         config.headers['IF-Match'] = etag;
      return config;
    },
   response: function (response) {
      if (response.config.method === 'GET')
          etag = reponse.config.headers['e-tag'];
        // Return the response or promise.
      return response || $q.when(response);
    },
  };
});
Sign up to request clarification or add additional context in comments.

9 Comments

How to get the If-Match Data, as it should be equal to E-Tag value from a Get Response? How can i read that value?
Print the config object in the http interceptor, it contains everything you need
I am able to add the If-Match value to the request, but still not clear about how to get the value from the response of a get method
I edited my answer (with pseudo code, I have not test it)
Yes, i have changed config to response in the response function, but all i am getting is NULL. I think the response function is properly getting the response form the server. Any ideas?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.