Philipp Spindler
Philipp Spindler

Reputation: 21

nginx read response header when multiple headers have the same name

I want to read the following response headers.

Response-Header:

...
set-cookie: somecookie=7ABFBB3446C856779B59A837DE3946F7DB; Path=/; Secure;
set-cookie: wantedcookie=12414ASDFAFADFW2342342; Path=/; Secure;
...

The variable $sent_http_set_cookie contains the first header

$sent_http_set_cookie = somecookie=7ABFBB3446C856779B59A837DE3946F7DB; Path=/; Secure;

--

How to read the second header in order to get the value of the wantedcookie?

The nginx configuration looks like this:

location /api {
  add_header X-Debug "${sent_http_set_cookie}";
}

Upvotes: 2

Views: 980

Answers (1)

HuDahai
HuDahai

Reputation: 47

I have the same problem with you. I solved it by using the following code

local headers = ngx.resp.get_headers()
ngx.log(ngx.ERR, " set-cookie type is ", type(headers['set-cookie']))
ngx.log(ngx.ERR, "cookie headers are", require("cjson").encode(headers["set-cookie"]))

if there are multiple headers with the same, then headers[name] is a table.

ps: the code block show be placed in header_filter phase.

Upvotes: 1

Related Questions