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;
}
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);
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.