React-redux-universal-hot-example: How to set a header?

Created on 6 Jun 2016  路  1Comment  路  Source: erikras/react-redux-universal-hot-example

Using the client middleware, how do I set a header. I'm talking about when you do a request in this way:

return {
  types: [LOGOUT, LOGOUT_SUCCESS, LOGOUT_FAIL],
  promise: (client) => client.get('/logout')
};

Thanks.

Most helpful comment

You'd have to modify ApiClient.js in order to accept headers but I did it in the following way:

return {
  types: [LOGOUT, LOGOUT_SUCCESS, LOGOUT_FAIL],
  promise: (client) => client.get('/logout', { headers: { [header-name]: [header-value], ... })
};

ApiClient.js

class ApiClient {
  constructor(req) {
    methods.forEach((method) => {
      this[method] = (path, { params, data, attach, field, headers, local, external = false } = {}) => new Promise((resolve, reject) => {
        const request = superagent[method](external ? path : formatUrl(path, local));
        const cookie = req.get("cookie");

        if (headers) {
          request.set(headers);
        }
...

>All comments

You'd have to modify ApiClient.js in order to accept headers but I did it in the following way:

return {
  types: [LOGOUT, LOGOUT_SUCCESS, LOGOUT_FAIL],
  promise: (client) => client.get('/logout', { headers: { [header-name]: [header-value], ... })
};

ApiClient.js

class ApiClient {
  constructor(req) {
    methods.forEach((method) => {
      this[method] = (path, { params, data, attach, field, headers, local, external = false } = {}) => new Promise((resolve, reject) => {
        const request = superagent[method](external ? path : formatUrl(path, local));
        const cookie = req.get("cookie");

        if (headers) {
          request.set(headers);
        }
...
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sunkant picture sunkant  路  4Comments

chrisabrams picture chrisabrams  路  5Comments

LarryEitel picture LarryEitel  路  4Comments

jorgehmv picture jorgehmv  路  3Comments

glennr picture glennr  路  6Comments