0

I am trying to send http get request along with headers as below

    var headers = {}
    headers['x-email'] = 1;
    headers['x-from'] = 1577836800;
    headers['x-to'] = 1598659200;


return this.http.get<any>(environment.apiUrl+'/User/getTrips',{headers: new HttpHeaders(headers)})
      .subscribe(data => {
        if (data == undefined) {
          this.rowData = [];
          this.errorMsg = data['Error'];
        } else {
          this.rowData = data.slice(1);
          this.gridOptions.rowData = data;
          this.gridOptions.api.refreshCells({force : true});
        }

When i see in network console, i could not see any headers in the request and hence am getting empty api response.

1 Answer 1

1

Try initializing the variable before using it.

  const httpHeaders: HttpHeaders = new HttpHeaders({
        'x-email': 1,
        'x-from': 1577836800
        'x-to': 1598659200
      });

and then

 this.http.get<any>(environment.apiUrl+'/User/getTrips',
    {headers: httpHeaders })//... code continues here

My initial thought is HttpHeaders class has a few subtleties since it is immutable. By changing the initialization this way we make sure it is using the correct type and that everything is properly initialized before invoking the http.get

btw this is not correct: *if data is undefined; there is no Error property

 if (data == undefined) {
          this.rowData = [];
          this.errorMsg = data['Error']; /* if data is undefined; there is no Error property*/
        } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks much. It worked. Yeah sorry, you are right, if data is undefined, there is no error property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.