Hello!
On Mon, Apr 1, 2013 at 10:47 AM, wrote:
> I was wondering if anyone could recommend for me the best way to consume an
> API response from an upstream proxy and then modify the contents of the
> response before sending it back out to the client.
>
Depending on your requirements, you can use either body_filter_by_lua*
or ngx.location.capture.
> I have a handler '/getdata' pointing to that upstream server:
>
> location /getdata {
> proxy_pass http://backend/getData.php
> }
>
> The uri on the upstream server is '/getData.php'.
>
What happens when you access this location /getdata directly from the
client side?
> I have another handler '/api' taking the incoming requests and passing it to
> the subrequest '/getdata' with arguments.
>
> location ~ /api$ {
> content_by_lua '
> local params = "param1=someval"
> local res = ngx.location.capture("/getdata" , { args = params })
> ngx.say(res)
> ';
> }
>
> I seem to be missing something since I can get back a valid response from
> the upstream server.
You mean you can *not* get back a valid response? What exactly do you
see? Can you print out "res.status" in your Lua code?
> The error log isn't helping me much even with rewrite_log on.
>
You can try enabling the Nginx debugging logs to get more detailed
information, as documented here:
http://wiki.nginx.org/Debugging#Debugging_log
> I want to get the data response back, decode the json, modify the output,
> and then create the json object for the client.
>
BTW, it's also considered good practice to provide more information
about the software that you're using, like the version of nginx and
ngx_lua, as well as the name of your operating system.
Best regards,
-agentzh