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

Adding headers in DataStore #39

Closed
Tinostarn opened this issue Oct 18, 2016 · 1 comment
Closed

Adding headers in DataStore #39

Tinostarn opened this issue Oct 18, 2016 · 1 comment

Comments

@Tinostarn
Copy link

Hello,

I'm trying to add headers to provide a token access to my API. The guide gives this line :

this.datastore.headers = new Headers({'Authorization': 'Bearer ' + accessToken});

and 2 other examples, but they seem to be on the consumer components side. I'd like to avoid specifying headers it in each of my HTTP calls.
First I tried to add it in a "headers" key within the @JsonApiDatastoreConfig decorator, but didn't work.

Then I tried to put it in my Datastore service, in the constructor :

export class Datastore extends JsonApiDatastore {

    constructor(http: Http) { 
        super(http);
        this.headers = new Headers({ 'Authorization': 'Bearer ' + localStorage.getItem('access_token')});
    }

}

Now something strange, each time I validate my form and check the headers that are sent to the server, it turns out that the "accept" and "Content-type" keys are repeated.
Example, if I submit my form 3 times, I get on the third call :

accept:application/vnd.api+json,application/vnd.api+json,application/vnd.api+json
authorization:Bearer NDgyZGQ1YTIzMTZiNjg4MzY0ZDVmOTk1NzkyZjZjZGNmZjIyZDZmMDU2YzY0NThmYzc5OGM1ZmE2ZTcxZWE3ZA
content-type:application/vnd.api+json,application/vnd.api+json,application/vnd.api+json

Does it come from the way I add my header ?
Just for information, when I submit a form, server doesn't return a valid response yet (test purpose), so the app is assumed to be crashed.

Thx for your help

@jamesdixon
Copy link

@supertino7 I'm also trying to do this in the constructor, but unfortunately, at the time the service/provider is initialized, the token isn't available yet and therefore headers are never set. How are you handling this?

Thank you!

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

2 participants