This is an automated email from the ASF dual-hosted git repository.
mhamann pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/incubator-openwhisk-apigateway.git
The following commit(s) were added to refs/heads/master by this push:
new 993dda2 Decode body only if it is JSON (#338)
993dda2 is described below
commit 993dda276e22121ed19f95eb3032084aa5471151
Author: Alex Song <[email protected]>
AuthorDate: Mon Apr 1 12:29:15 2019 -0400
Decode body only if it is JSON (#338)
---
scripts/lua/policies/mapping.lua | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/scripts/lua/policies/mapping.lua b/scripts/lua/policies/mapping.lua
index f778e28..8b42130 100644
--- a/scripts/lua/policies/mapping.lua
+++ b/scripts/lua/policies/mapping.lua
@@ -20,7 +20,7 @@
local logger = require "lib/logger"
local utils = require "lib/utils"
-local cjson = require "cjson"
+local cjson = require "cjson.safe"
local _M = {}
@@ -53,7 +53,15 @@ end
function getRequestParams()
ngx.req.read_body()
body = ngx.req.get_body_data()
- body = (body and cjson.decode(body)) or {}
+ if body ~= nil then
+ -- decode body if json
+ decoded, err = cjson.decode(body)
+ if err == nil then
+ body = decoded
+ end
+ else
+ body = {}
+ end
headers = ngx.req.get_headers()
path = ngx.var.uri
query = parseUrl(ngx.var.backendUrl)
@@ -184,7 +192,7 @@ function transformAllParams(s, d)
end
function finalize()
- if next(body) ~= nil then
+ if type(body) == 'table' and next(body) ~= nil then
local bodyJson = cjson.encode(body)
ngx.req.set_body_data(bodyJson)
end