Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f9b897d

Browse files
danielstocktonIgorMinar
authored andcommitted
feat($http): add a default content type for PATH requests
The default header is now application/json which while not perfect in all cases is better than the browser default application/xml. The new headers also makes for better compatibility with Rails 4
1 parent 1f99c3a commit f9b897d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/ng/http.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ function isSuccess(status) {
123123
function $HttpProvider() {
124124
var JSON_START = /^\s*(\[|\{[^\{])/,
125125
JSON_END = /[\}\]]\s*$/,
126-
PROTECTION_PREFIX = /^\)\]\}',?\n/;
126+
PROTECTION_PREFIX = /^\)\]\}',?\n/,
127+
CONTENT_TYPE_APPLICATION_JSON = {'Content-Type': 'application/json;charset=utf-8'};
127128

128129
var defaults = this.defaults = {
129130
// transform incoming response data
@@ -147,8 +148,9 @@ function $HttpProvider() {
147148
common: {
148149
'Accept': 'application/json, text/plain, */*'
149150
},
150-
post: {'Content-Type': 'application/json;charset=utf-8'},
151-
put: {'Content-Type': 'application/json;charset=utf-8'}
151+
post: CONTENT_TYPE_APPLICATION_JSON,
152+
put: CONTENT_TYPE_APPLICATION_JSON,
153+
patch: CONTENT_TYPE_APPLICATION_JSON
152154
},
153155

154156
xsrfCookieName: 'XSRF-TOKEN',
@@ -340,7 +342,7 @@ function $HttpProvider() {
340342
*
341343
* A custom default cache built with $cacheFactory can be provided in $http.defaults.cache.
342344
* To skip it, set configuration property `cache` to `false`.
343-
*
345+
*
344346
*
345347
* # Interceptors
346348
*
@@ -873,8 +875,8 @@ function $HttpProvider() {
873875

874876

875877
if ((config.cache || defaults.cache) && config.cache !== false && config.method == 'GET') {
876-
cache = isObject(config.cache) ? config.cache
877-
: isObject(defaults.cache) ? defaults.cache
878+
cache = isObject(config.cache) ? config.cache
879+
: isObject(defaults.cache) ? defaults.cache
878880
: defaultCache;
879881
}
880882

test/ng/httpSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,15 @@ describe('$http', function() {
684684
$httpBackend.flush();
685685
});
686686

687+
it('should set default headers for PATCH request', function() {
688+
$httpBackend.expect('PATCH', '/url', 'messageBody', function(headers) {
689+
return headers['Accept'] == 'application/json, text/plain, */*' &&
690+
headers['Content-Type'] == 'application/json;charset=utf-8';
691+
}).respond('');
692+
693+
$http({url: '/url', method: 'PATCH', headers: {}, data: 'messageBody'});
694+
$httpBackend.flush();
695+
});
687696

688697
it('should set default headers for custom HTTP method', function() {
689698
$httpBackend.expect('FOO', '/url', undefined, function(headers) {

0 commit comments

Comments
 (0)