<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: hela sp</title>
    <description>The latest articles on DEV Community by hela sp (@helasp).</description>
    <link>https://dev.to/helasp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2558080%2F3eacfefb-b950-4fa8-b2f9-ba3ea5093be3.png</url>
      <title>DEV Community: hela sp</title>
      <link>https://dev.to/helasp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/helasp"/>
    <language>en</language>
    <item>
      <title>Azure functions isolated worker HTTP trigger: custom header disappears from response.</title>
      <dc:creator>hela sp</dc:creator>
      <pubDate>Thu, 12 Dec 2024 08:54:01 +0000</pubDate>
      <link>https://dev.to/helasp/azure-functions-isolated-worker-http-trigger-custom-header-disappears-from-response-c32</link>
      <guid>https://dev.to/helasp/azure-functions-isolated-worker-http-trigger-custom-header-disappears-from-response-c32</guid>
      <description>&lt;p&gt;So I had a simple Azure function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public async Task&amp;lt;HttpResponseData&amp;gt; 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);   
// ctx.GetCorrelationId() is an extension method on the FunctionContext 
    response.Headers.Add("x-correlationId", ctx.GetCorrelationId());        
    return response;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I stopped on the line &lt;code&gt;return response&lt;/code&gt; with debugger, I saw that the response did indeed contain the custom header 'x-correlationId'. However, the client (I tried Postman and browser) did not receive this header. &lt;/p&gt;

&lt;p&gt;The solution was to first insert the custom header, and &lt;em&gt;then&lt;/em&gt; write the body into it. So the sequence is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;response.Headers.Add("x-correlationId", ctx.GetCorrelationId());
await response.WriteAsJsonAsync(result);    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unfortunately, I don't know what was really happening here. Why the debugger showed me that the header was there even though the response was somehow modified by the &lt;code&gt;WriteAsJsonAsync&lt;/code&gt; method.&lt;/p&gt;

</description>
      <category>azurefunctions</category>
    </item>
  </channel>
</rss>
