Hello!
On Sat, Sep 14, 2013 at 3:57 AM, Eugene Ware wrote:
> I wish to do the same thing as the example, but also pass through HTTP POST
> parameters.
>
> I've managed to get access to the POST variables in lua, but I can't work
> out how to pass them through to the postgres_query function.
>
[...]
>
> Are you able to provide an example of passing through HTTP POST variables to
> postgres_query? Thanks in advance.
>
In short, you should use rewrite_by_lua instead of content_by_lua
here, because content_by_lua conflicts with postgres_pass in a single
nginx location block (both of them correspond to a content handler
while a single location can only have one content handler).
Below is a tested example to demonstrate this usage:
location = /t {
# The following two directives should take the same value
# so that nginx will always buffer the whole request body
# in memory:
client_body_buffer_size 8k;
client_max_body_size 8k;
set $quoted_id '';
rewrite_by_lua '
ngx.req.read_body()
local args = ngx.req.get_post_args()
local id = args.id
if not id then
ngx.status = 400
ngx.say("ERROR: No id specified.")
return ngx.exit(400)
end
-- you need to install the ngx_set_misc module for the following
-- line of code:
ngx.var.quoted_id = ndk.set_var.set_quote_pgsql_str(id)
';
postgres_query 'select * from cats where id = $quoted_id';
postgres_pass backend;
postgres_output rds;
rds_json on;
}
Here're the test results on my side (assuming the Nginx listens on the
8080 port):
$ curl -i -d 'id=3' localhost:8080/t
HTTP/1.1 200 OK
Server: nginx/1.4.2
Date: Sat, 14 Sep 2013 19:48:07 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
[{"id":3,"name":"bob"}]
$ curl -i -d 'id=2' localhost:8080/t
HTTP/1.1 200 OK
Server: nginx/1.4.2
Date: Sat, 14 Sep 2013 19:48:40 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
[{"id":2,"name":null}]
$ curl -i -d 'foo=2' localhost:8080/t
HTTP/1.1 400 Bad Request
Server: nginx/1.4.2
Date: Sat, 14 Sep 2013 19:49:19 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
ERROR: No id specified.
Here I'm using curl's -d option to specify the POST request body on
the command line.
Please note that you need to install at least 4 modules to your Nginx:
ngx_lua, ngx_set_misc, ngx_postgres, and ngx_rds_json. If you're using
the openresty bundle, then you can have all these at once :)
Best regards,
-agentzh