View in #cornerstone3d on Slack
@Christopher_Nagy: Hi, I am using the @cornerstone/nifti-image-loader package, and was wondering if I can somehow configure the headers for the requests done to fetch the images? I would like to add headers like a Bearer token to authenticate to my server.
@Rod_Fitzsimmons_Frey: I just worked through this! The “options” passed to the loader init function has a “beforeSend” hook that you can set to return custom headers.
For dicom-image-loader, my code looks like this:
import { init as dicomImageLoaderInit } from "@cornerstonejs/dicom-image-loader";
...
/**
* Function to pass to cornerstone's wadourlloader, which gets called before every
* XHR call to load an image. Returns a Record of headers to be set, in our case the auth header.
*/
const beforeSend = (token: string) => {
const value = "Bearer " + token;
const handler = () => {
return { authorization: value };
};
return handler;
};
...
// during my app initialization. accessToken
// is a JWT
const options: LoaderOptions = {
beforeSend: beforeSend(session.user.accessToken),
errorInterceptor,
};
...
await dicomImageLoaderInit(options);
I haven’t checked, but I imagine the same system is used for the nifti loader.
Whelp, I looked at the nifti-loader and I was wrong, it doesn’t seem to have any opportunity to set custom headers. An opportunity to contribute, I suppose!
However, I noticed that internally it’s using fetch to get the url, so you could consider replacing the global fetch with a wrapper that adds your headers. Something like this:
const oldFetch = fetch;
window.fetch = async (input, init = {}) => {
init = init || {};
// Add the Authorization bearer token
const token = "whatshouldwedotodaybrain?sameaseverydaypinkie";
init.headers = {
...init.headers,
Authorization: `Bearer ${token}`,
};
// Bob's yer uncle
return oldFetch(input, init);
};
@Alireza_Sedghi: i think we need to have a proper way to configure this similar to how we do beforeSend in wado
can you create an issue for us?
@Christopher_Nagy: https://github.com/cornerstonejs/cornerstone3D/issues/1688
#1688 [Feature Request] nifti-volume-loader should be configurable to include custom (auth) headers in requests (beforeSend
)
@Rod_Fitzsimmons_Frey thanks for the tip, the workaround works!
@Alireza_Sedghi: This is done in this PR, you can use it natively https://github.com/cornerstonejs/cornerstone3D/pull/1708
#1708 [OHI-1443] feat(nifti-loader): Add custom header support to nifti loader to support auth headers or any custom headers