Search code examples
angularposthttp-headers

Angular - Cannot set HttpHeader key Origin


I do calling POST API in localhost. Because of some requirements from server, I have to modify Origin key in header by a real URL, but it seems still show default Origin key. I can change Content-Type but Origin is not. ;)

My function is:

createItem() {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json',
         'Origin': 'mywebsite'
      })
    };
    const url = environment.apiUrl;
    return this.http.post(url, data, httpOptions);
}

But in Network tab, it still show Origin value as locahost

Content-Type: application/json
Origin: http://localhost:4300 // I want my url here

Solution

  • You cant:

    As described on MDN; Origin is a 'forbidden' header, meaning that you cannot change it programmatically.

    MDN Reference Page

    Another question related to this one, with further info