Hello!
On Thu, Sep 20, 2012 at 11:00 AM, Antoine wrote:
>
> Before asking a very quick question, I'd like to thank you for your work
> nginx-related, it's efficient and quite well documented. :)
>
Glad you like it :)
> I'm currently trying to get specific fields of the response headers.
> If I do curl -v http://localhost/myurl, I get everything I want (body +
> headers) of course.
> But if I do res=location.capture("/myurl"), I get the body thanks to
> res.body, but I can't figure out how to get the content of the header.
> I do get res.header (which is a table, according to Lua), but I don't know
> what's in it,
You can use Lua's for loop structure to iterate through the res.header table:
http://www.lua.org/manual/5.1/manual.html#2.4.5
Here is a complete example:
location = /t {
content_by_lua '
res = ngx.location.capture("/sub")
for k, v in pairs(res.header) do
if type(v) == "table" then
ngx.say(k, ": ", table.concat(v, ","))
else
ngx.say(k, ": ", v)
end
end
';
}
location = /sub {
content_by_lua '
ngx.header.foo = {32, 56, "hey"}
ngx.header.bar = "hello world"
ngx.say("hello world")
';
}
And we can access /t like this:
$ curl localhost/t
Content-Type: text/plain
foo: 32,56,hey
bar: hello world
> and how to get specific fields like "Etag" or "Date" (nil
> value), even though I'm sure that they are returned somehow because curl
> does show them !
>
There is a "-i" option for curl that can display the response headers :)
I'm cc'ing the openresty-en mailing list to let more users see our
discussion here: https://groups.google.com/group/openresty-en You're
also very welcome to join us there ;)
Thanks!
-agentzh