Azure functions isolated worker HTTP trigger: custom header disappears from response. – DEV Community
December 12, 2024

Azure functions isolated worker HTTP trigger: custom header disappears from response. – DEV Community

So I have a simple Azure function like this:

public async Task GetCountries(
    [HttpTrigger(AuthorizationLevel.Function, "get", Route = "countries")] HttpRequestData req, FunctionContext ctx)
{
    var result = await _countriesService.GetCountries();            
    var response = req.CreateResponse(HttpStatusCode.OK);    
    await response.WriteAsJsonAsync(result);    
    response.Headers.Add("x-correlationId", ctx.GetCorrelationId());        
    return response;
}
Enter full screen mode

Exit full screen mode

when i stop return response Using the debugger, I see that the response does contain the custom header “x-correlationId”. However, the client (I tried Postman and browser) does not receive this header.

The solution is to insert the custom header first and then Then Write the text in it. So the order is:

response.Headers.Add("x-correlationId", ctx.GetCorrelationId());
await response.WriteAsJsonAsync(result);    
Enter full screen mode

Exit full screen mode

Unfortunately, I have no idea what’s going on here. Why does the debugger show me the headers are there even though the response was modified in some way? WriteAsJsonAsync method.

2024-12-12 08:54:01

Leave a Reply

Your email address will not be published. Required fields are marked *