Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response headers #9

Closed
svnlto opened this issue Sep 23, 2015 · 8 comments
Closed

response headers #9

svnlto opened this issue Sep 23, 2015 · 8 comments

Comments

@svnlto
Copy link

svnlto commented Sep 23, 2015

I'm looking for a way to get ahold of the response headers from a POST request. How would I go about this?

@agraboso
Copy link
Owner

Currently, you cannot do it. What is your use case?

@svnlto
Copy link
Author

svnlto commented Sep 24, 2015

Need to handle data I'm getting back through 'Authorization' headers.

@agraboso
Copy link
Owner

Are you talking about the WWW-Authenticate and Proxy-Authenticate headers as per RFC 7235 section 4.1? We could support something very restricted and well-justified like this, but I wouldn't want to clutter redux-api-middleware by making it pass all response headers back to the client.

@svnlto
Copy link
Author

svnlto commented Sep 25, 2015

but I wouldn't want to clutter redux-api-middleware by making it pass all response headers back to the client.

Well, technically you're doing that already since fetch has the headers constructor on the response object you're getting back from your xhr.

I do however, see where you're coming from with this. Perhaps adding a new config key which determines which headers are being passed on as part of the payload could be an option?

export function fetchUser(userId, schema = userSchema) {
  return {
    [CALL_API]: {
      types: ['FETCH_USER.REQUEST', 'FETCH_USER.SUCCESS', 'FETCH_USER.FAILURE'],
      endpoint: `/users/${userId}`,
      method: 'GET',
      headers: { 
        request: {
          credentials: 'same-origin'
        },
        response: ['Authorization', 'X-SomeToken']
      }
    },
    payload: { somePayload }
  };
}

Thoughts?

@agraboso
Copy link
Owner

That sounds good, but I'd need to rearrange a few things. I'll let you know soonish.

@seokgyo
Copy link

seokgyo commented Oct 7, 2015

I also need this feature. real-world example in redux also get 'link' data from header.
I think it would be great if it's possible to process headers in transform().

@agraboso
Copy link
Owner

Solved by v1.0.0-beta1.

@caleb-harrelson
Copy link

caleb-harrelson commented Mar 14, 2019

For others looking at how to handle custom headers in the response, here's how I did mine.

  • Wrap redux-api-middleware with a middleware that adds additional logic to RSAAs.
  • That middleware wraps success RSAAs to add a meta property with your handling.
  • You can then either return the header in the meta so it's available to all reducers, or do your handling globally in the meta function (as I do below).

Here's a simplified version of my success wrapper, which checks a response header to see if the server wants the client to force a reload (after a deploy) and passes that client-version off to a redux action:

const generateSuccessType = (type, dispatch) => {
	return {
		type,
		meta: (action, state, res) => {
			let version = res.headers.get('X-Client-Version')
			if (version) {
				dispatch(checkClientVersion(version))
			}
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants