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

Commit

Permalink
feat($http): add a default content type for PATH requests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
danielstockton authored and IgorMinar committed May 16, 2013
1 parent 1f99c3a commit f9b897d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ function isSuccess(status) {
function $HttpProvider() {
var JSON_START = /^\s*(\[|\{[^\{])/,
JSON_END = /[\}\]]\s*$/,
PROTECTION_PREFIX = /^\)\]\}',?\n/;
PROTECTION_PREFIX = /^\)\]\}',?\n/,
CONTENT_TYPE_APPLICATION_JSON = {'Content-Type': 'application/json;charset=utf-8'};

var defaults = this.defaults = {
// transform incoming response data
Expand All @@ -147,8 +148,9 @@ function $HttpProvider() {
common: {
'Accept': 'application/json, text/plain, */*'
},
post: {'Content-Type': 'application/json;charset=utf-8'},
put: {'Content-Type': 'application/json;charset=utf-8'}
post: CONTENT_TYPE_APPLICATION_JSON,
put: CONTENT_TYPE_APPLICATION_JSON,
patch: CONTENT_TYPE_APPLICATION_JSON
},

xsrfCookieName: 'XSRF-TOKEN',
Expand Down Expand Up @@ -340,7 +342,7 @@ function $HttpProvider() {
*
* A custom default cache built with $cacheFactory can be provided in $http.defaults.cache.
* To skip it, set configuration property `cache` to `false`.
*
*
*
* # Interceptors
*
Expand Down Expand Up @@ -873,8 +875,8 @@ function $HttpProvider() {


if ((config.cache || defaults.cache) && config.cache !== false && config.method == 'GET') {
cache = isObject(config.cache) ? config.cache
: isObject(defaults.cache) ? defaults.cache
cache = isObject(config.cache) ? config.cache
: isObject(defaults.cache) ? defaults.cache
: defaultCache;
}

Expand Down
9 changes: 9 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,15 @@ describe('$http', function() {
$httpBackend.flush();
});

it('should set default headers for PATCH request', function() {
$httpBackend.expect('PATCH', '/url', 'messageBody', function(headers) {
return headers['Accept'] == 'application/json, text/plain, */*' &&
headers['Content-Type'] == 'application/json;charset=utf-8';
}).respond('');

$http({url: '/url', method: 'PATCH', headers: {}, data: 'messageBody'});
$httpBackend.flush();
});

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

2 comments on commit f9b897d

@petebacondarwin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, the commit message should be PATCH not PATH.

@pisaacs
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amen for this fix, was pulling what little hair I had left: rails/rails#11155 ... and especially since in AngularJS you can't set (yet) the HTTP PATCH header (ideally, $httpProvider.defaults.headers.patch = 'application/json';)

Please sign in to comment.