0

I have a problem with retrieve header value from my .get request.

In my API I have Max-range:65 - this values tell me how much items exist in DB. Because I get them by 10 I'is very important to know how much items exist on server. How Can I grab this information in my request? It's look a like:

In my service I has a method:

  public getRequest(params, refresh?: boolean): Observable<any> {
        const getUrl = '/coreapi/request';
        return this.http
            .get(getUrl, {params})
            .map((result: Obj[]) => {
                for (const item of result) {
                    const unit = new Test(
                        item.id,
                        item.value);

                    this.unitSet.push(unit);
                }
                return this.unitSet;
            });
    }

And in component I subscribe this like:

    this.sub
        .do(() => {
            // do something
        })
        .distinctUntilChanged()
        .switchMap((data: any) => {
            return this.service.getRequest(data.httpParams, data.refresh);
        })
        .subscribe((result: Unit[]) => {
            this.units = result;
        });

---- EDIT

But I get only this headers from list of 12:

{"pragma" => "pragma"},
{"content-type" => "content-type"},
{"cache-control" => "cache-control"},
{"expires" => "expires"}

Do you know how to grab custom headers?

kris_IV
  • 2,396
  • 21
  • 42
  • 1
    Per this previous answer, you need to add the `Access-Control-Expose-Headers` response header to your API - the value should be a comma-delimited set of custom response header names you want to expose to the client-side. This is what will allow your client-side code to access any custom response headers besides the standard browser ones, which is all you're able to retrieve just now. https://stackoverflow.com/a/40855050/3617262 – Stevangelista Jan 28 '18 at 22:00

1 Answers1

0

I found an answer but it's really not clear in Angular docs.

Now default http request has default option: observe: 'body', so response doesn't return headers.

To get body & headers this we should set observe as 'response'.

So in my example I should use:

const observeVal = 'response'
return this.http
   .get(this.apiUrl + getCurses, {params: params, observe: observeVal})

When default is:

const observeVal = 'body'
return this.http
   .get(this.apiUrl + getCurses, {params: params, observe: observeVal})
kris_IV
  • 2,396
  • 21
  • 42