Faas: Question: How can I set custom headers on function responses?

Created on 31 Dec 2017  路  3Comments  路  Source: openfaas/faas

It would be nice if functions could somehow override returned headers on a per-instance basis.

Expected Behaviour

I would like to override the returned content-type of a function based on the incoming content-type. If I need to support multiple content-types, and the only difference in a function that returns JSON vs. one that returns XML is 1-2 lines, having the ability to set the content-type header based on the request header would be useful. Sure, I can build multiple functions, but if I'm working in Rust for instance, then the only way forward I can see would be splitting the bulk of the logic into a library module, using that module from several binaries, then kicking off several builds per content-type. And, while this focuses on content-type, maybe there's a more generic use case for setting any arbitrary return headers--Cloudflare/CDN-specific headers, for instance? There may be footgun opportunities there, but if someone decides to walk that path then I assume they know the risks.

Current Behaviour

The current behavior seems to let you set a single content-type per function. I agree with the recent change that doesn't automatically match the request and response content-types, but I think there should be an alternative to the current model, and something that allows for setting any returned header would be great.

Possible Solution

Maybe draft an enhanced output format that can be enabled by setting a fwatchdog environment variable? This might:

  • Accept specially-formatted JSON of the form {"headers: [...], "body": ...}.
  • Treat everything before the first blank line as "Header: value" pairs, and everything after as the body.

I don't ultimately care what the format is, and maybe something like protobuf/GRPC might be worth a look. I also think the current model is fine to keep, but a more advanced opt-in would be useful.

Context

I'm trying to build a simple comment system for static sites. If someone GETs the function wanting text/html, I'd like to output a simple comment form, along with a list of recent comments. POSTs would add a comment. For folks wanting more control, GETs requesting JSON would return individual comments or threads. I can do this in half a dozen different functions, but much of the logic for each use case is the same, down to the function return which simply spits out different data based on the content-type. I'm also pretty busy, so the easier this is to accomplish, the more likely I am to do it. :)

Thanks! I'm having fun with this platform.

question

Most helpful comment

This would be useful, however it is not an easy problem to solve.

It would be nice if functions could somehow override returned headers on a per-instance basis.

If I understand this right by "per instance" meaning "per function" then you can already override Content-Type etc through watchdog environmental variables.

We have some work in the AfterBurn mode which may allow for custom headers to be set but for the time being unless priorities change or you are willing to fund the development of the feature then you will need to find a way to work around the current constraints.

The best solution may be to have common code and multiple functions or a small micro-service that stands in front of your functions and changes the content-type as needed.

All 3 comments

This would be useful, however it is not an easy problem to solve.

It would be nice if functions could somehow override returned headers on a per-instance basis.

If I understand this right by "per instance" meaning "per function" then you can already override Content-Type etc through watchdog environmental variables.

We have some work in the AfterBurn mode which may allow for custom headers to be set but for the time being unless priorities change or you are willing to fund the development of the feature then you will need to find a way to work around the current constraints.

The best solution may be to have common code and multiple functions or a small micro-service that stands in front of your functions and changes the content-type as needed.

You can now do this with the of-watchdog and the new generation of templates. Example: https://github.com/openfaas-incubator/node8-express-template

Derek close: inactive

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexellis picture alexellis  路  4Comments

maxbry picture maxbry  路  7Comments

matthewdolman picture matthewdolman  路  5Comments

hotjunfeng picture hotjunfeng  路  5Comments

alexellis picture alexellis  路  5Comments