Appendix: Extract Response Headers#

To extract headers from a response, you can wrap the fetch in another function that extracts response headers.

For example:

import { Session } from "@inrupt/solid-client-authn-browser";

import { getSolidDataset } from "@inrupt/solid-client";

const session = new Session();

// ... log the session in.

const customizeHeaders = (
  customizedFetch,
  requestHeaders,
  bindResponseHeaders,
) => {
  return async (info, init) => {
    const response = await customizedFetch(info, {
      ...init,
      headers: {
        ...init?.headers,
        ...requestHeaders,
      },
    });
    bindResponseHeaders(response.headers);
    return response;
  };
};

let responseHeaders = new Headers();

const customFetch = customizeHeaders(
  session.fetch,
  {"x-request-id": "7492595229158059"},
  (headers) => {
    // Bind the headers to the current scope.
    responseHeaders = headers;
  },
);
await getSolidDataset(
   "https://storage.example.com/7865026e-5450/some-resource",
   { fetch: customFetch }
);
console.log(responseHeaders.get("x-request-id"));

See also: