how to modify response based on request using lua?

424 views
Skip to first unread message

Gaurav Kumar

unread,
Nov 14, 2014, 9:05:31 PM11/14/14
to openre...@googlegroups.com
I know I can independently access request and response in lua. What I want is to access response body conditionally - and the condition is based on request. For example, if the request method is POST, then only I want to modify response (say, add header or modify body). How do I go about it?  

Yichun Zhang (agentzh)

unread,
Nov 15, 2014, 12:49:51 AM11/15/14
to openresty-en
Hello!
header_filter_by_lua and body_filter_by_lua can be your friends here :)

But be careful about streaming processing in the body filter.

Regards,
-agentzh

Gaurav Kumar

unread,
Nov 15, 2014, 1:40:56 AM11/15/14
to openre...@googlegroups.com

Thanks  Agentzh!  Actually http method was just an example, I would need to conditionally process response based on post request parameters or even get request paramters. I don't think header filter will help. Anyways the bigger question is even if I can use access request filter or something similar directive, how do I combine the two directives? In other words how do I make sure body_filter_by_lua is being invoked in response to a specific request only. If someone can provide a pseudo code that would be awesome.

--
You received this message because you are subscribed to a topic in the Google Groups "openresty-en" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openresty-en/UEG8cDqdlcc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yichun Zhang (agentzh)

unread,
Nov 15, 2014, 2:08:39 AM11/15/14
to openresty-en
Hello!

On Fri, Nov 14, 2014 at 10:40 PM, Gaurav Kumar wrote:
> Thanks Agentzh!

Please do not capitalize my nick. Thank you.

> Actually http method was just an example, I would need to
> conditionally process response based on post request parameters or even get
> request paramters. I don't think header filter will help.

It can surely help here.

> Anyways the bigger
> question is even if I can use access request filter or something similar
> directive, how do I combine the two directives? In other words how do I make
> sure body_filter_by_lua is being invoked in response to a specific request
> only.

You can set flags in ngx.ctx in access_by_lua, for example, and make
decisions in header/body_filter_by_lua. The header/body_filter_by_lua
directives themselves are very cheap so it's totally fine that you
just check the flags in ngx.ctx and do nothing there.

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages