Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4828

How to override the adapter headers that get sent in the ajax request?

$
0
0

@LuisAverhoff wrote:

I came across something weird behaviour a couple of days ago in ember-data(I’m using 3.12 without ember-fetch or ember-ajax).

So I have this headers object in my application adapter that gets sent in almost all of the requests I make in my application.

// Application adapter
export default Component.extend({
  headers: {
   Accept: "application/json" // Majority of my requests will accept json content
  }
  ... Other properties and method go here.
})

What I’m trying to accomplish is to override the accept header from application/json to text/csv because there is a method in my application that downloads the content I receive from the server as csv. This is how I’m doing it.

saveAsCSV() {
  const options = {
    headers: {Accept: "text/csv"} // I want this request to use text/csv because we are downloading content as csv in this method.
  }
  
  this.store.adapterFor("Application").ajax(this.url, "GET",  options).then((response) => {
     // Do something with the response
  })
}

Unfortunately this doesn’t do what I expect. The request headers will still have application/json as the accept type. This is because of what happens here https://github.com/emberjs/data/blob/master/packages/adapter/addon/rest.js. In particular, options.headers = assign({}, options.headers, headers); Here headers is the headers from the application adapter in my case.

To me this particular behavior is not what I expect and maybe someone can explain to me why it is done like this. To me, the headers in any adapter are headers that meant to be applied to a majority of your requests but can be overridden when you pass in headers to the ajax method. Not that the headers in the adapter can override the headers that I provided to the ajax method.

How should I proceed to get the desired behavior?

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4828

Trending Articles