Add HTTP headers to RESTAdapter ajax calls #803

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
10 participants

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.

Docs at the head of the rest_adapter.js file?

hashg commented Mar 18, 2013

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

+
+ ```js
+ DS.RESTAdapter.reopen({
+ apiHeaders: [[ "API_KEY", "secret key"], ["ANOTHER_HEADER", "asdsada"]]
@pangratz

pangratz Mar 28, 2013

Member

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"
}
+ for (var i = 0; i < headers.length; i++) {
+ xhr.setRequestHeader(headers[i][0], headers[i][1]);
+ }
+ };
@pangratz

pangratz Mar 28, 2013

Member

... and then here something like:

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

adambird Apr 8, 2013

much cleaner, will make update accordingly

Member

pangratz commented Mar 28, 2013

This seems like a useful addition 👍

Contributor

trabus commented Apr 8, 2013

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.

Contributor

arbales commented Apr 8, 2013

What makes these apiHeaders vs. headers? The latter is more guessable and simple and I'd favor it unless there's any important semantic or technical reason not too.

Sent from my thumbs.

On Apr 7, 2013, at 10:26 PM, Jake Bixby notifications@github.com wrote:

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.


Reply to this email directly or view it on GitHub.

adambird commented Apr 8, 2013

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.

Owner

tomdale commented Apr 10, 2013

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

+1 for headers.

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

Member

bradleypriest commented Apr 25, 2013

@adambird Can you squash your commits into one please

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
Owner

tomdale commented May 11, 2013

Happy to merge once this rebases cleanly.

Contributor

dblock commented Jul 3, 2013

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

@wagenet wagenet closed this Aug 10, 2013

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