Angular 4/5 HttpClient: Argument of type string is not assignable to 'body'

前端 未结 4 1462
醉酒成梦
醉酒成梦 2020-11-30 05:58

The Angular docs say:

The response body doesn\'t return all the data you may need. Sometimes servers return special headers or status codes to indic

相关标签:
4条回答
  • 2020-11-30 06:18

    Typescript complains about this problem

    Type 'string' is not assignable to type "body"

    To solve this, convert string to body manually. Example:

        const httpOptions = {
          headers: new HttpHeaders({
            'Content-Type': 'application/json'
          }),
          observe: 'response' as 'body'
        };
        return this.http.post<any>(url, data, httpOptions);
    
    0 讨论(0)
  • 2020-11-30 06:22

    The way I got around this, without inline-ing the options (which can lead to code that's not as clean) was to create an interface for the request options. Code looks like this :

    export interface IRequestOptions {
        body?: any;
        headers?: HttpHeaders | { [header: string]: string | Array<string> };
        observe?: any;
        params?: HttpParams | { [param: string]: string | Array<string> };
        reportProgress?: boolean;
        responseType?: "arraybuffer" | "blob" | "json" | "text";
        withCredentials?: boolean;
    }
    

    Then this is used as such :

    const options: IRequestOptions = {
        headers: new HttpHeaders({"Content-Type": "application/json"}),
        observe: "response"
    };
    return this.httpClient.post(`${environment.USER_SERVICE_BASE_URL}`,
        {"username": credentials.username, "password": credentials.password}, options)
        .pipe(
            map((res: HttpResponse<any>) => ...
        );
    

    Change for original post to use lettable or pipeable (whatever the current name is today) operators

    0 讨论(0)
  • 2020-11-30 06:29
    import { HttpHeaders, HttpParams } from '@angular/common/http';
    export interface IRequestOptions {
        headers?: HttpHeaders | { [header: string]: string | string[]; };
        observe: "response"; 
        params?: HttpParams | { [param: string]: string | string[]; };
        reportProgress?: boolean; 
        responseType?: "json";
        withCredentials?: boolean; 
    }
    
    0 讨论(0)
  • 2020-11-30 06:32

    You have to inline the options. See github ticket #18586, entry by alxhub on August 9 2017.

    Typescript needs to be able to infer the observe and responseType values statically, in order to choose the correct return type for get(). If you pass in an improperly typed options object, it can't infer the right return type.

    login(credentials: Credentials): Observable<any> {
        return this.httpClient.post<any>(`${environment.USER_SERVICE_BASE_URL}`,
          {'username': credentials.username, 'password': credentials.password}, {
          headers: new HttpHeaders({'Content-Type': 'application/json'}),
          observe: 'response'
        })
          .map((res) => ...
    
    0 讨论(0)
提交回复
热议问题