Kérian Pelat
Kérian Pelat

Reputation: 121

Angular - Headers for request post

I have a problem with the headers of my request post I want to add a project with name and description but when I try to post my form I have this error:

Argument of type 'RequestOptions' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'headers' are incompatible. Type 'Headers' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'. Type 'Headers' is not assignable to type '{ [header: string]: string | string[]; }'. Index signature is missing in type 'Headers'.

And in my console i have this : 401 (Unauthorized)

The code for this (all is good but this part nop) :

addProject(token: string, id: number, nom: string, capteurs): Observable<ProjetModel> {    
  const headers = new Headers({'X-Auth-Token': token});
  const body = { nom, capteurs };
  const options = new RequestOptions({ headers: headers });
  return this.http.post(`${environment.baseUrl}/projets`, body, options).map((response: Response) => response.json());
}

Thanks,

Upvotes: 0

Views: 612

Answers (2)

Anto Antony
Anto Antony

Reputation: 872

Try to add the headers as follows

     import { HttpClient } from '@angular/common/http';
     import { HttpHeaders } from '@angular/common/http';

     @Injectable()
     export class ServiceName {

        constructor(private http: HttpClient) { }

        addProject(token: string, id: number, nom: string, capteurs): Observable<ProjetModel> {    

           const httpOptions = {
             headers: new HttpHeaders({
               'Authorization': 'my-auth-token'
             })
           };

           return this.http.post(`${environment.baseUrl}/projets`, body, httpOptions ).map((response: Response) => response.json());
        }
    }

Upvotes: 1

K&#233;rian Pelat
K&#233;rian Pelat

Reputation: 121

With this code :

addProject(token: string, id: number, nom: string, capteurs): Observable<ProjetModel> {
    const headers = new HttpHeaders({'Authorization': token});
    const body = { nom, capteurs };
    const options = new RequestOptions({ headers: headers });
    return this.http.post(`${environment.baseUrl}/projets`, body, httpOptions).map((response: Response) => response.json());
}

I have this error : ERROR in src/app/projet.service.ts(25,40): error TS2345: Argument of type '{ headers: HttpHeaders; }' is not assignable to parameter of type 'RequestOptionsArgs'.

Types of property 'headers' are incompatible.

Type 'HttpHeaders' is not assignable to type 'Headers'.

  Property 'forEach' is missing in type 'HttpHeaders'.

src/app/projet.service.ts(26,67): error TS2304: Cannot find name 'httpOptions'.

src/app/projet.service.ts(26,84): error TS2345: Argument of type '(response: Response) => any' is not assignable to parameter of type '(value: ArrayBuffer, index: number)=> any'.

Types of parameters 'response' and 'value' are incompatible.

Type 'ArrayBuffer' is not assignable to type 'Response'.

  Property 'type' is missing in type 'ArrayBuffer'.

Upvotes: 0

Related Questions