1

I want to get a date from XHR Header Reponse.

Response Header

I tried to add '{observe: 'response'}' as options

 constructor(private http: HttpClient) { }
    getAllTemps() {
        return this.http.get<AllTemp>(this.endpoint, {observe: 'response'});
      }

but then my response looks like this: enter image description here There is no souch key as I expected.

Any ideas how to solve this? Maybe there is another way?

Mateusz Daniluk
  • 115
  • 2
  • 9

1 Answers1

1

If you want to get the Date from XHR-Header in your Angular application. First your back-end application should send that header in the response using res.set() if it's node.js. Same way in other server langauge.

You can get header in the following way

getAllTemps() {

    return this.http.get<AllTemp>(this.endpoint, { observe: 'response' })
      .subscribe(res => {
        // getting all the response headers provided by server application
        console.log(res['headers'].keys());

        // Getting header with key
        console.log(res['headers].get('Date'));

          });
  }

Note: While getting header by key and if your server-application didn't send header it'll log null

find following stackblitz example. working stackblitz demo with dummy url

Chaitanya
  • 847
  • 1
  • 8
  • 15
  • First check whether you're having `Date` header in the response using `console.log(res['headers'].keys());` – Chaitanya Aug 03 '19 at 10:32
  • I use PHP on my backend. I tried to add header `header('Time: '. $current_date = date('H:i:s'));` but it still don't show me headers. – Mateusz Daniluk Aug 04 '19 at 17:06
  • There is no problem in `Angular` code which I provided. You're not setting the `Date` header correctly in your `php`. I think it should be header `('Date':...)`. Also check my first comment, to verify you're getting `Date` in the log array. – Chaitanya Aug 04 '19 at 19:09
  • @MateuszDaniluk added the stackblitz demo, which gets `expires` header. As that API not exposing `Date` header I given example with `expires`. – Chaitanya Aug 05 '19 at 06:58