Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add HTTP headers to RESTAdapter ajax calls #803

Closed
wants to merge 1 commit into from

10 participants

@adambird

Some APIs require additional HTTP headers, for example an API_KEY so have added the ability for this.

Followed the same pattern as used for namespace by checking for an apiHeaders property on the adapter.

Example configuration.

DS.RESTAdapter.reopen({ 
  apiHeaders: [[ "API_KEY", "secret key"], ["ANOTHER_HEADER", "asdsada"]]
});

I couldn't find tests for constructing the ajax hash so was unsure where to start. Would appreciate any guidance.

@adambird

Docs at the head of the rest_adapter.js file?

@hashg

This should be easy going forward. Content-Type: application/jsonp

packages/ember-data/lib/adapters/rest_adapter.js
((21 lines not shown))
+ An adapter can target other hosts by setting the `url` property.
+
+ ```js
+ DS.RESTAdapter.reopen({
+ url: 'https://api.example.com'
+ });
+ ```
+
+ ### API Headers customization
+
+ Some APIs require HTTP headers, eg to provide an API key. An array of
+ headers can be added to the adapter which are passed with every request:
+
+ ```js
+ DS.RESTAdapter.reopen({
+ apiHeaders: [[ "API_KEY", "secret key"], ["ANOTHER_HEADER", "asdsada"]]

The two dimensional array looks a little strange to me. What do you think of using an object?

apiHeaders: {
  "API_KEY": "secret key",
  "X-ANOTHER-HEADER": "blablu"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
packages/ember-data/lib/adapters/rest_adapter.js
@@ -325,6 +360,15 @@ DS.RESTAdapter = DS.Adapter.extend({
hash.data = JSON.stringify(hash.data);
}
+ if (this.apiHeaders !== undefined) {
+ var headers = this.apiHeaders;
+ hash.beforeSend = function (xhr) {
+ for (var i = 0; i < headers.length; i++) {
+ xhr.setRequestHeader(headers[i][0], headers[i][1]);
+ }
+ };

... and then here something like:

var apiHeaders = this.get('apiHeaders');
Ember.keys(apiHeaders).forEach(function(key) {
  xhr.setRequestHeader(key, apiHeaders[key]);
});
@adambird
adambird added a note

much cleaner, will make update accordingly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pangratz

This seems like a useful addition :+1:

@trabus

This would be awesome, and I second pangratz's version of it. I'm currently using $.ajaxSetup({ beforeSend: .... to do this manually, so having something integrated into ember-data would be nice.

@arbales
@adambird

My motivation for using apiHeaders v headers was just an attempt to be explicit. These are headers required by the API. So with that in mind happy to go with the consensus.

@tomdale
Owner

I'm with @arbales; I like this change but would prefer headers to apiHeaders.

@pootsbook

+1 for headers.

@adambird

Sorry for the delay, have made the update to change the name and method of describing the headers adambird@b8bb97e

@bradleypriest
Collaborator

@adambird Can you squash your commits into one please

@adambird adambird supported adding HTTP headers to RESTAdapter ajax calls
add docs for customisation options for RESTAdapter

renamed apiHeaders to headers and used an object rather than array of arrays to describe headers
132e453
@tomdale
Owner

Happy to merge once this rebases cleanly.

@dblock

I think this can be closed, #1068 is the merged version of this implementation.

@wagenet wagenet closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 25, 2013
  1. @adambird

    supported adding HTTP headers to RESTAdapter ajax calls

    adambird authored
    add docs for customisation options for RESTAdapter
    
    renamed apiHeaders to headers and used an object rather than array of arrays to describe headers
This page is out of date. Refresh to see the latest.
Showing with 47 additions and 0 deletions.
  1. +47 −0 packages/ember-data/lib/adapters/rest_adapter.js
View
47 packages/ember-data/lib/adapters/rest_adapter.js
@@ -59,6 +59,44 @@ var get = Ember.get, set = Ember.set, merge = Ember.merge;
}
}
```
+
+ ## Customization
+
+ ### Endpoint path customization
+
+ Endpoint paths can be prefixed with a `namespace` by setting the namespace
+ property on the adapter:
+
+ ```js
+ DS.RESTAdapter.reopen({
+ namespace: 'api/1'
+ });
+ ```
+ Requests for `App.Person` would now target `/api/1/people/1`.
+
+ ### Host customization
+
+ An adapter can target other hosts by setting the `url` property.
+
+ ```js
+ DS.RESTAdapter.reopen({
+ url: 'https://api.example.com'
+ });
+ ```
+
+ ### Headers customization
+
+ Some APIs require HTTP headers, eg to provide an API key. An array of
+ headers can be added to the adapter which are passed with every request:
+
+ ```js
+ DS.RESTAdapter.reopen({
+ headers: {
+ "API_KEY": "secret key",
+ "ANOTHER_HEADER": "asdsada"
+ }
+ });
+ ```
*/
DS.RESTAdapter = DS.Adapter.extend({
bulkCommit: false,
@@ -325,6 +363,15 @@ DS.RESTAdapter = DS.Adapter.extend({
hash.data = JSON.stringify(hash.data);
}
+ if (this.headers !== undefined) {
+ var headers = this.headers;
+ hash.beforeSend = function (xhr) {
+ Ember.keys(headers).forEach(function(key) {
+ xhr.setRequestHeader(key, headers[key]);
+ });
+ };
+ }
+
jQuery.ajax(hash);
},
Something went wrong with that request. Please try again.