0

Hello I am having an angular 5 application and spring boot java backend that supports REST api calls. I am adding a button in my angular component template , upon on clicking the button it should send a request to the java backend and download a file. However it is not working .

enter image description here

so in the above screen when user clicks the csv button, what i would expect is to download a csv file . here is my component html code for that . I am only placing the relevant code

<div class="col-2 text-right">
    <pc-button theme="primary"  [externalLink]="generateCSV(formGroup.value)" [disabled]="formGroup.invalid">
        <i class="fas fa-download mr-2"></i>
        {{ 'ACTIONS.CSV' | translate }}
    </pc-button>

This is my component typscript code.

generateCSV(formValue: MessageHistoryFormValue) {

const token = this.authenticationService.token;

let params: HttpParams = new HttpParams();
params.append('access_token', token);
params.append('startDate', formValue.range[0].toISOString());
params.append('endDate', formValue.range[1].toISOString());
params.append('cdrStatus', formValue.cdrStatus);
params.append('msisdn', formValue.msisdn);
params.append('accountId',  formValue.account?formValue.account.id.toString(): null);
params.append('inventoryCode',  formValue.account?formValue.account.id.toString(): null);
params.append('campaignId',  formValue.campaign.id? formValue.campaign.id.toString(): null);
params.append('flightId',  formValue.flight?formValue.flight.id: null);
params.append('pageNumber',  String(this.pageNumber));
params.append('pageSize',  String(this.pageSize));

return this.messageHistoryService.getMessageHistoriesCSV(params);

}

the following is the method definition defined in my service class.

@Injectable()
export class MessageHistoryService {
 getMessageHistoriesCSV(params: HttpParams){
    console.log(params.toString );
    return `${this.config.api}/message-history/csv?${params.toString()}`;
  }

}

However i am getting 401 because the request i send dont passing the parameters.

enter image description here

This is my java backend code to handle that request.

@RestController
@RequestMapping("/api")
public class MessageHistoryReportingResource {

        @GetMapping("/message-history/csv")
        @Timed
        @Secured({ AuthoritiesConstants.CAMPAIGN_MANAGER, AuthoritiesConstants.TEAM_MEMBER,
                 AuthoritiesConstants.GLOBAL_ADMIN })
        public ResponseEntity<byte[]>  getMessageHistoriesCSV(MessageHistoryFilterRequest messageHistoryFilterRequest)
                throws IOException {
            return ResponseEntity.ok().body(
                    CsvUtil.createByteArray(this.messageHistoryReportingService.findCSV(messageHistoryFilterRequest)));
        }

    }

I am getting 401 because the parameters that i set in HttpParams are not going to the server. any idea how can i fix it . Really appreciate any help

0 Answers0