如何使用Nginx和Lua处理POST请求的JSON主体? [英] How can I manipulate the JSON body of a POST request using Nginx and Lua?
问题描述
我正在做一个概念验证,以演示如何在堆栈中实现 3scale .在一个示例中,我想执行一些POST request 正文操作,以创建一个API外观,该外观将旧API格式映射为新的内部API格式.例如.更改类似
I am doing a proof of concept to demonstrate how we might implement 3scale in our stack. In one example I want to do some POST request body manipulation to create an API façade that maps what might be a legacy API format to a new internal one. Eg. change something like
{ "foo" : "bar" , "deprecated" : true }
进入
{ "FOO" : "bar" }
content_by_lua 的 Lua模块文档,适当的方法说
The Lua module docs for content_by_lua, which seems like the appropriate method say
请勿在同一位置使用此指令和其他内容处理程序指令.例如,该指令和 proxy_pass 指令不得用于相同的位置.
Do not use this directive and other content handler directives in the same location. For example, this directive and the proxy_pass directive should not be used in the same location.
我的理解是content_by_lua是类似于proxy_pass的内容处理程序,每个位置只能使用其中一个.
My understanding is that the content_by_lua is a content handler like proxy_pass, only one of which can be used per location.
我认为无法删除 proxy_pass ,因为这是代理工作方式的基础,因此可以使用 content_by_lua 来将请求捕获到一个单独的位置 strong>,然后转到实现 proxy_pass 的位置,或者是否有其他方法,例如 strong> rewrite_by_lua 哪个更合适?
I don't think there's any way to remove proxy_pass as that's the basis of how the proxying works, so is it possible capture the request in a separate location, use content_by_lua, then pass to the location implementing proxy_pass or is there a different method like rewrite_by_lua which is more appropriate?
如果对其他人有帮助,我添加了以下函数(我的Lua的第一位),该函数删除了3scale要求授权的user_key
参数,但如果在以下位置转发,则对我们的API无效:
If it helps anyone else, I added the following function (my first bit of Lua) which removes the user_key
parameter which 3scale requires for authorization but is invalid for our API if forwarded on:
function remove_user_key()
ngx.req.read_body()
-- log the original body so we can compare to the new one later
local oldbody = ngx.req.get_body_data()
log(oldbody)
-- grab the POST parameters as a table
local params = ngx.req.get_post_args()
-- build up the new JSON string
local newbody = "{"
for k,v in pairs(params) do
-- add all the params we want to keep
if k ~= "user_key" then
log("adding"..k.." as "..v)
newbody = newbody..'"'..k..'":"'..v..'",'
else
log("not adding user_key")
end
end
--remove the last trailing comma before closing this off
newbody = string.sub(newbody, 0, #newbody-1)
newbody = newbody.."}"
ngx.req.set_body_data(newbody)
log(newbody)
end
if ngx.req.get_method() == "POST" then
remove_user_key()
end
推荐答案
我建议您使用access_by_lua
在nginx.conf
I will suggest you to use access_by_lua
in nginx.conf
location / {
#host and port to fastcgi server
default_type text/html;
set $URL "http://$http_host$request_uri";
access_by_lua_file /home/lua/cache.lua;
proxy_pass http://$target;
-------
---------
在cache.lua文件中,您可以执行以下操作:
in cache.lua file you can do something like :
if ngx.req.get_method() == "POST" then
-- check if request method is POST
-- implement your logic
return
end
这篇关于如何使用Nginx和Lua处理POST请求的JSON主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!