d.ansimov
d.ansimov

Reputation: 2171

Nginx put POST/GET request body to variable

I have an openresty server with a single location, in which I need to RPUSH POST/GET request body to the redis DB.

Here's a config snippet:

map $request_method $query {
    "GET"           $request;
    "POST"          $request_body;
}

...

location /test {
    redis2_query rpush $key $query;
    redis2_pass redis6379:6379;
}

But, if it gets POST request, $query seems to be empty.

access_by_lua 'ngx.req.read_body()';

and

echo_read_request_body;

doesn't seems to help. Thanks in advance for any kind of help.

Upvotes: 1

Views: 9784

Answers (1)

d.ansimov
d.ansimov

Reputation: 2171

set $query "";

rewrite_by_lua '
    local method = ngx.var.request_method
    if method == "POST" then
        ngx.req.read_body()
        local data = ngx.req.get_body_data()
        ngx.var.query = data.."&ng_ua="..ngx.var.http_user_agent.."&ng_ip="..ngx.var.remote_addr.."&ng_ip="..ngx.var.time_local
    elseif method == "GET" then
        local data = ngx.var.query_string
        ngx.var.query = data.."&ng_ua="..ngx.var.http_user_agent.."&ng_ip="..ngx.var.remote_addr.."&ng_ip="..ngx.var.time_local
    end
';

Upvotes: 2

Related Questions