Kvoti / redux-rest

Automatically create Redux action constants, action creators, and reducers for your REST API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do I set custom header?

bhoomit opened this issue · comments

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

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.

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?

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.

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

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.

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

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

I was looking in Chrome Network window.

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
#7 (comment).

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

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

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

Great!

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

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

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
#7 (comment).

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.

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
#7 (comment).

Yes!