简体   繁体   中英

Http post response issue in Angular 8

I am trying to get download the server response content as csv. While doing the same I have getting 2 errors.

Method given below:

generateCsv(exportModel: any) {
    let headers={};
    return this.http.post<any>(ReportConstant.exportApi, exportModel,
        { observe:"response",responseType: 'blob'})
} 

The error:

ERROR in src/app/components/export/_services/export.service.ts(22,91): error TS2322: Type '"response"' is not assignable to type '"body"'.

src/app/components/export/_services/export.service.ts(22,110): error TS2322: Type '"blob"' is not assignable to type '"json"'.

I am very new with front end technologies. Kindly suggest me the solution to resolve the issue.

     return  this.httpClient.post(url,
            body,
            {
                headers: new HttpHeaders({
                    'Content-Type': 'application/json'
                }),
                observe: 'response',
                responseType: 'blob'
            }
        );

The way you are using the POST API(Http Client) is wrong

If you are passing more data to server use POST or else GET should do the Job

POST: import statemenet in case you are doubtful

import {HttpClient, HttpHeaders, HttpParams, HttpParameterCodec} from "@angular/common/http";

add this in your constructor

private httpClient: HttpClient

Code starts:

let body = new HttpParams({encoder: new CustomEncoder()});
body = body.append('args', 'stringArguments')); //Add needed arguments

//Set header and response types

let options= {
      headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'),
      responseType:'text' as 'json'
    };

// This will return a promise(observable) which can be called utilized as 
// observableReturn.subscribe(response => this.ProcessAsyncResponse(response), error => this.HandleSRProcessError(error, this.serviceRequestCallList));

return this.httpClient.post(ajaxUrl, body, options)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM