CustomResponseHeader modification using go template

Hi,
I am trying to update an existing header using a middleware with customResponseHeader.

This is my config using File provider and a yaml file:

http:
  middlewares:
    updateCookieHeaders:
      headers:
        customResponseHeaders:
          X-Custom-Header: "{{ `Hello World` }} {{  printf `%#v` . }}"
          Set-Cookie: "{{ if $cookie := .Response.Header.Get `Set-Cookie` }} {{ $cookie}}; SameSite=Strict {{ end }}"

Spoiler: it does NOT work :frowning:

Yes the middleware is applied and called. Removing the Set-Cookie header modification does set my X-Custom-Header.

Printing the Go template context in my X-Custom-Header reveals that it's false. But I thought it should be the Response object.

What am I missing here?

Is it even possible to do such modifications?

That's Traefik's log output:

time="2023-02-17T10:47:31+01:00" level=error msg="Error occurred during watcher callback: /etc/traefik/conf/common.yml: template: :33:49: executing \"\" at <.Response.Header.Get>: can't evaluate field Response in type bool" providerName=file

Hello,

The go template can use inside the file but not inside a field inside the file.
The go template is interpreted during the load of the configuration and not during the request.
So you cannot use information related to the request or the response.

From my memories, there is a plugin to handle what you are trying to achieve.

https://plugins.traefik.io/plugins

Ah OK, this makes sense.
I will have a look at the plugins.
Thanks for the explanation

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.