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)
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)
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);
},
};
});