Can I change X-Tyk-Cached-Response header name?

Hello Tyk Team,

I am trying to add the X-Tyk-Cached-Response response header value to a new one calle X-Cache

Reading documentation and issue in the forum I tried the next configuration:

,
"transform_response_headers": [
  {
    "add_headers": {
      "x-cached": "$tyk_context.headers_X-Tyk-Cached-Response"
    },
    "path": "(.*)",
    "method": "GET"
  }
]

I added too:

"response_processors": [
        {
            "name": "header_injector"
        }
    ]

Tyk is creating the header but without value :frowning:

Is it a bug or a configuration problem?

Thanks in advance :slight_smile:

Have you turned on the context variables?

https://tyk.io/docs/context-variables/
If not using a Tyk Dashboard, add the field enable_context_vars to your API definition file at root level and set it to true.

Also per this doc, it should be β€œ_” not β€œ-” here $tyk_context.headers_X-Tyk-Cached-Response

Also - this context variable is for the request values, not response:

Context variables are extracted from the request at the start of the middleware chain, and must be explicitly enabled in order for them to be made available to your transforms.

Hi Yaara,

thanks for your response but it is not working, I think the context variables in response headers don’t work as in request headers.

Have you seen both of my answers?

  1. THe format of the header was wrong
  2. You cannot access headers in the response context, only in the request context, but you CAN refer to the request context in the response headers middleware.

This is my definition:

X-Yaara: $tyk_context.headers_X_Random_Header

and the api call:

 curl http://tyk-gateway.localhost:8080/httpcodes/headers -H "X-Random-Header: 12345" -v
...
< X-Yaara: 12345
....

Hello Yaara,
I read the first one, we wrote more or less at the same moment the second comment.

  1. THe format of the header was wrong

Sure, I realized the format some days ago and I changed.

  1. You cannot access headers in the response context, only in the request context, but you CAN refer to the request context in the response headers middleware.

Ok, all clear, Yaara, thanks for your time.

1 Like