Read request headers in Azure Functions v4 with Node

I often have to read information from request headers in Azure Functions. For example to get the User Object ID that made an authenticated call. This is a bit more tricky than just calling request.headers[‘x-ms-client-principal-id‘]. This is valid TypeScript but will always be undefined!

Turns out you first have to invoke the .entries() method on the request.headers, so here’s a quick helper method:

import { HttpRequest } from "@azure/functions";
export function readHeader(request: HttpRequest, key: string): string {
return Object.fromEntries(request.headers.entries())[key];
}
view raw read-header.ts hosted with ❤ by GitHub

So now you can simply get a header value by calling  
const userId = readHeader(request, ‘x-ms-client-principal-id‘);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.