Я пытаюсь обновить данные через свой API с помощью метода http Patch. Но я получаю ошибку или внутреннюю ошибку сервера.
Вот мой звонок JSON:
$http.patch(baseUrl + '/users/' + currentUserEmail,data).success(success).error(error)
Вы можете добавить необходимые заголовки, используя необязательные 3-е параметры $ http.patch:
var config = {headers: {'IF-Match': 'your-data'}};
$http.patch(baseUrl + '/users/' + currentUserEmail,data, config).success(success).error(error)
В документации содержится информация о настраиваемых параметрах конфигурации.
Если вы хотите добавить пользовательские заголовки для каждого запроса автоматически, вы можете использовать перехватчик $ http:
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: ответить на ваш комментарий о том, как получить информацию от запроса GET. В перехватчике http вы также можете перехватить ответ:
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);
},
};
});