Aurelia-fetch client creates request headers on the fly

I use aurelia-fetch-client to send some data to the web-api (in the register method).

headers: Headers;

register() {

    this.headers = new Headers();

    this.headers.append("content-type", "application/json; charset=utf-8");

    this.httpClient.fetch("api/Account/Register", {
        method: "POST",
        body: JSON.stringify({
            email: this.email,
            password: this.password
        }),

        headers: this.headers
    })
}

As you can see, I want to update the headers of my request (in this call to the append method), and for this I need to create my own Headers object, call the append method on it, and then assign its title property to my request. I want to do this directly in the request body: instead of writing

 headers: this.headers

I want to write something like:

 headers: { 
    append("content-type", "application/json; charset=utf-8");
 }

or something like:

  headers: new Headers().append(..)

The idea is to avoid declaring a new object to store my headers. How can i do this?

Thank you.

+4
source share
1 answer

JS headers:

this.httpClient.fetch("api/Account/Register", {
    method: "POST",
    body: JSON.stringify({
        email: this.email,
        password: this.password
    }),

    headers: {
       "content-type", "application/json; charset=utf-8"
    }
});

headers, :

this.httpClient.fetch("api/Account/Register", {
    method: "POST",
    body: JSON.stringify({
        email: this.email,
        password: this.password
    }),

    headers: new Headers({
       "content-type", "application/json; charset=utf-8"
    })
});

. .

+8

All Articles