Superagent: Set multiple headers at once?

Created on 12 May 2017  路  5Comments  路  Source: visionmedia/superagent

Sorry if this has been addressed - searching came up with nothing. Unless I am missing something, looks like each custom header must be individually using .set(). It would come in very handy to be able set all headers at once as an object or array of objects. Something like this:

const headers = {"Content-Type": "application/json", "Accept": "application/json"};
// OR 
const headers = [{"Content-Type": "application/json"}, {"Accept": "application/json"}];

.setAll(headers)

We have a lot of custom headers and they can be conditional on many things. Now we have to pass a superagent request object to a custom method and have it call .set() on whatever headers it wants to add based on various conditions and dependencies. It's not the most elegant thing.

Most helpful comment

Hey it looks like this actually works!

.set({"Content-Type":"application/json","Accept":"application/json","Margle":"Bargle"})

Problem solved! Would be good to add to the docs I guess.

All 5 comments

You can use Object.assign to merge headers conditionally.

Hey it looks like this actually works!

.set({"Content-Type":"application/json","Accept":"application/json","Margle":"Bargle"})

Problem solved! Would be good to add to the docs I guess.

Not working as i am trying the same

Not working as i am trying the same

@stoffern Maybe late but be sure that your WAF is not filtering custom headers.

Very late :D i solved it somehow 馃殌

Was this page helpful?
0 / 5 - 0 ratings

Related issues

t3hmrman picture t3hmrman  路  9Comments

srohde picture srohde  路  8Comments

SergioFaya picture SergioFaya  路  5Comments

noway picture noway  路  4Comments

jvanalst picture jvanalst  路  10Comments