Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom headers to Azure Functions response

I'm working on an Azure Function App (v2), which has a number of different functions inside the same Function App.

It is going to be deployed in multiple Azure regions, and I would like to add a custom header, that indicates which region served the request.

I know that I can let each function return HttpResponseMessage which can contain custom headers. Instead of duplicating that code to each function, I would like to make it centralized in the entire Function App project. Another drawback of returning HttpResponseMessage is that I would like to use IActionResult, so I can use JsonResult or OkObjectResult and similar return types.

With an ASP.NET MVC project, I could add a middleware which added the header or add the headers using web.config. Neither seems to be possible in a Function App.

How can this be done without having to return HttpResponseMessage and adding the header inside each function's Run method?

like image 847
RasmusW Avatar asked Jan 16 '19 12:01

RasmusW


People also ask

Can we set response header?

You can set response headers, you can add response headers. And you can wonder what the difference is. But think about it for a second, then do this exercise. Draw a line from the HttpResponse method to the method's behavior.

Can Azure function return HTML?

Even though Azure Functions is mainly used to serve APIs and return JSON objects, it can also be used to help HTML content.

Can you trigger an azure function using an HTTP request?

The HTTP trigger lets you invoke a function with an HTTP request. You can use an HTTP trigger to build serverless APIs and respond to webhooks. The default return value for an HTTP-triggered function is: HTTP 204 No Content with an empty body in Functions 2.


2 Answers

I was able to do this by accessing the HttpResponse object via the request's HttpContext.

For example:

req.HttpContext.Response.Headers.Add("ETag", entity.ETag);
return new OkObjectResult(entity);

produces: enter image description here

like image 191
bvpb Avatar answered Oct 21 '22 17:10

bvpb


while researching the exact same problem, I came along this blog post. Basically, you derive a custom class fomr JsonResult, OKResult and so on and manipulate the ExecuteResult method and add your headers there.

public class ServiceUnavailableResult : StatusCodeResult {
    private readonly int _retryAfterHeaderValue;

    public ServiceUnavailableResult(int retryAfterHeaderValue) : base(500) {
        _retryAfterHeaderValue = retryAfterHeaderValue;
    }

    public override void ExecuteResult(ActionContext context) {
        base.ExecuteResult(context);
        
        context.HttpContext.Response.Headers.Add("Retry-After", _retryAfterHeaderValue.ToString());
    }
}

It does not fully help you, but if you only use like two or three classes when returning content, it might.

like image 2
Claas Diederichs Avatar answered Oct 21 '22 17:10

Claas Diederichs