Postman gives a success response while the same API throws error when called through angular...

Discussão em 'Angular' iniciado por ONE_FE, Fevereiro 13, 2025.

  1. ONE_FE

    ONE_FE Guest

    I'm getting some info from API. All the postman calls are success. Please find the attachment below.

    [​IMG]

    When I call the same API in my angular application, I'm getting below error from back-end:

    [​IMG]

    [​IMG]

    Component file

    ngOnInit() {
    this.getOffers();
    }

    getOffers(){
    this._offerService.getOffers()
    .pipe(first())
    .subscribe(
    data => {
    if (data.CODE == 401){
    console.log("Session not valid");
    }
    this.offers = <Offers[]>data;
    console.log("offers response ",data);

    },
    error => {
    console.log("An Error Occurred ", error);
    });
    }


    Service File :

    getOffers(): Observable<any>{
    const token = this.authService.getToken();
    console.log("Offer to ", token);

    let headerOptions = new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': 'Bearer '+token
    });
    let options = { headers: headerOptions };

    const url = environment.baseURL + 'promotions';

    return this._http.get<any>(url, options)
    .pipe(map(offers => {
    return offers;
    }));

    }


    What am I doing wrong here?

    Continue reading...

Compartilhe esta Página