ONE_FE
ONE_FE

Reputation: 996

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

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

enter image description here

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

enter image description here

enter image description here

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 I'm doing wrong here ?

Upvotes: 0

Views: 770

Answers (1)

Freeky
Freeky

Reputation: 820

This could be an allowed hosts problem, since angular sends requests from a different port of your address.

Let me explain better:

In backend code there could be an allowed hosts setting which allows or handles requests from 192.168.1.4 (which postman may use to send requests) but not 192.168.1.4:4200 (which angular uses to send requests)

And the response error may differ if handled by backend code

Upvotes: 2

Related Questions