Kvoti / redux-rest

Automatically create Redux action constants, action creators, and reducers for your REST API
MIT License
180 stars 11 forks source link

How do I set custom header? #7

Closed bhoomit closed 8 years ago

bhoomit commented 8 years ago

I have an API request where I need to set custom header. How do I do that?

mallison commented 8 years ago

You can pass an options object to the Flux constructor (which I really must rename). One property is setCSRF which takes a function which is passed the request object, so you can set headers there (again that property could be named better). E.g

function setHeaders(request) {
    request.set('Header', 'value');
    // you must return the request object (just realised this is a bug to fix too)
    return request;
}

const api = new Flux({
    setCSRF: setHeaders
});

(I wrote this library early on in my redux/flux days and, while it works, it needs some love to bring it up to standard)

Hope that helps.

bhoomit commented 8 years ago

Hi @mallison , thanks for responding :)

This is what I'm doing and its not working.

function setHeaders(request) {
      console.log('called')
      request.set('X-USER-ID', '1')
      // you must return the request object (just realised this is a bug to fix too)
      return request
  }

// Then create a Flux instance. This automatically creates
// action creators and reducers for each endpoint. No boilerplate!
const flux = new Flux({
  setCSRF: setHeaders,
  orders: '/api/orders/'
})

What am I doing wrong here?

mallison commented 8 years ago

No problem. It's entirely possibly it's my problem, not yours. I should be able to take a look this morning and see what's going on, as your code looks correct.

bhoomit commented 8 years ago

Cool. I'm also trying to understand how it works. Thanks!

mallison commented 8 years ago

I've had a quick look, and added a test to check the function you pass in can set headers. It seems to work. Are you looking at the request in your web server and not seeing the header? I'll try a real example to see if it works in the browser.

mallison commented 8 years ago

Stupid me. I didn't update the build dir when I published the last version. Will do that now. Sorry!

mallison commented 8 years ago

Updated to a new alpha release: please re-install and try again.

bhoomit commented 8 years ago

I was looking in Chrome Network window.

mallison commented 8 years ago

Have you tried the latest build now?

I merged the csrf stuff to master and updated the build so things should be working.

On 1 March 2016 at 09:44, Bhoomit notifications@github.com wrote:

I was looking in Chrome Network window.

— Reply to this email directly or view it on GitHub https://github.com/Kvoti/redux-rest/issues/7#issuecomment-190637504.

bhoomit commented 8 years ago

Now the problem is, it doesn't get called in "GET" requests. I understood your code. Should I make that change?

mallison commented 8 years ago

Please do. Patches welcome ;). And apologies for the trouble. Though I'm glad someone is using it and finding these issues!

bhoomit commented 8 years ago

:) Happy to do it. If you have list of todos please post it in README. Will do if I find some time.

mallison commented 8 years ago

Great!

(One todo is actually a big rewrite now I'm a lot more experienced with redux, to make things feel more like redux)

bhoomit commented 8 years ago

Well I just started with Redux. So give me a week. Lets catch up after that.

mallison commented 8 years ago

No problem. Have fun!

On 1 March 2016 at 10:27, Bhoomit notifications@github.com wrote:

Well I just started with Redux. So give me a week. Lets catch up after that.

— Reply to this email directly or view it on GitHub https://github.com/Kvoti/redux-rest/issues/7#issuecomment-190652678.

bhoomit commented 8 years ago

How about I take out setCSRF outside and make it more generic like setHeaders? and have one generic method like prepareRequest which gets called before every request to add headers, CSRF, etc.

mallison commented 8 years ago

That's a good idea. So the default just sets the CSRF header, but the user can pass in their own function to modify the request however they like.

On 1 March 2016 at 10:34, Bhoomit notifications@github.com wrote:

How about I take out setCSRF outside and make it more generic like setHeaders? and have one generic method like prepareRequest which gets called before every request to add headers, CSRF, etc.

— Reply to this email directly or view it on GitHub https://github.com/Kvoti/redux-rest/issues/7#issuecomment-190654717.

bhoomit commented 8 years ago

Yes!