Send headers in angular http request

Asked

Viewed 307 times

2

I need to send three values in my header in http request, but when I check in the browser I realize that my headers have not been sent.

I tried something like:

  trocaToken(token):Observable<any>{
    const _headers = new HttpHeaders();
    const headers = _headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('token', token);
    headers.append('Authorization',  'bearer ' + token);
    return this._http.post<Usuario>(AppSettings.API_ENDPOINT + 'admin/detalhes',
    {headers: headers})
  }
  • 1

    When the request is Post the First given after the comma is Body, Tries to do: trocaToken(token):Observable<any>{&#xA; const _headers = new HttpHeaders();&#xA; const headers = _headers.append('Content-Type', 'application/x-www-form-urlencoded');&#xA; headers.append('token', token);&#xA; headers.append('Authorization', 'bearer ' + token);&#xA; return this._http.post<Usuario>(AppSettings.API_ENDPOINT + 'admin/detalhes', { } , &#xA; {headers: headers})&#xA; }

  • you are using http.post or httpClient?

  • 1

    @Leandroangelo by the method used (post with typing in <>), she is using Httpclient.

  • @mutlei Well observed

1 answer

0

Try it this way:

  trocaToken(token):Observable<any>{
     const httpOptions = {
     headers: new HttpHeaders({
        Content-Type': 'application/x-www-form-urlencoded',
        'token': token
     })
  };

  return this.http.post<Usuario>(AppSettings.API_ENDPOINT + 'admin/detalhes', undefined, httpOptions)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.