You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/04/18 07:28:44 UTC
[GitHub] [apisix] sniper-xx opened a new issue #4074: request help: How to rewrite the data of backend server response?
sniper-xx opened a new issue #4074:
URL: https://github.com/apache/apisix/issues/4074
### Issue description
I want to developing a plugin of apisix to fulfill unified format encapsulation. for example:
```json
{
"status": 200,
"msg": "ok",
"data": "datas real response from backend server..."
}
```
I writed a plugin , override the _M.body_filter method, but the funcation is not work correct. the code:
```lua
function _M.body_filter(conf, ctx)
core.log.error("------------------body_filter--------->")
local result = {
msg="ok",
code=status_code,
success=true
}
local isFinished = ngx.arg[2]
local resp_code = tonumber(status_code)
if not isFinished then
backend_content = backend_content..ngx.arg[1]
core.log.error("reading:----------------> ",string.len(backend_content))
ngx.arg[2] = false
end
if isFinished then
--ngx.arg[2] = false
core.log.error("end:----------------> ",ngx.arg[1])
core.log.error("ending:======================> ",string.len(backend_content))
--local r = "{\"mystatus\":200,\"data\":"..backend_content.."}";
result['data'] = cjson_decode(backend_content)
--ngx.arg[2] = true
ngx.arg[1] = cjson_encode(result)
ngx.arg[2] = true
end
end
```
the error is:
1、sometimes the client browser is not receive integrated json ;
2、the cjson_decode is error, printed error is : Expected end but to_obejct_begin.
My Question is how to implements the uniform data form wrap? please.
### Environment
* apisix version (cmd: `apisix version`):
* OS (cmd: `uname -a`):
* OpenResty / Nginx version (cmd: `nginx -V` or `openresty -V`):
* etcd version, if have (cmd: run `curl http://127.0.0.1:9090/v1/server_info` to get the info from server-info API):
* apisix-dashboard version, if have:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] sniper-xx commented on issue #4074: request help: How to rewrite the data of backend server response?
Posted by GitBox <gi...@apache.org>.
sniper-xx commented on issue #4074:
URL: https://github.com/apache/apisix/issues/4074#issuecomment-822111340
@Firstsawyou The problem has resovled. the reason is:
for the large json response, the _M.body_filter will execute many times. I store every ngx.arg[1] to variable backend_content, but the ngx.arg[1] not clear, so ,the final result is repeated.
solution:
```lua
if not isFinished then
backend_content = backend_content..ngx.arg[1]
**ngx.arg[1] = nil**
end
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander closed issue #4074: request help: How to rewrite the data of backend server response?
Posted by GitBox <gi...@apache.org>.
spacewander closed issue #4074:
URL: https://github.com/apache/apisix/issues/4074
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] sniper-xx commented on issue #4074: request help: How to rewrite the data of backend server response?
Posted by GitBox <gi...@apache.org>.
sniper-xx commented on issue #4074:
URL: https://github.com/apache/apisix/issues/4074#issuecomment-821948172
Or how get the upstream server addr and rewrited request uri, I send subrequest to get data from backend server in phase of rewrite or access. the resovlution is ok?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Firstsawyou commented on issue #4074: request help: How to rewrite the data of backend server response?
Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #4074:
URL: https://github.com/apache/apisix/issues/4074#issuecomment-821956134
How to rewrite the backend server response data, you can try to use the `response-rewrite` plugin,
https://github.com/apache/apisix/blob/master/docs/en/latest/plugins/response-rewrite.md
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] tokers commented on issue #4074: request help: How to rewrite the data of backend server response?
Posted by GitBox <gi...@apache.org>.
tokers commented on issue #4074:
URL: https://github.com/apache/apisix/issues/4074#issuecomment-822107779
> @Firstsawyou I refer to this plugin to write a new plugin. there are tow defferences:
> 1、 The ‘body' which is not static, is from the backend service;
> 2、The body content is very large. function of body_filter executed many times;
> I try to concatenate the chunck of every body_filter function received from backend,but can't not correct serialize to integrated json document.
I took a glance of your custom `_M.body_filter`, where is your `backend_content` variable? Also, when the response body is large, the string concatenation is inefficient.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Firstsawyou commented on issue #4074: request help: How to rewrite the data of backend server response?
Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #4074:
URL: https://github.com/apache/apisix/issues/4074#issuecomment-822112126
> @Firstsawyou The problem has resovled. the reason is:
> for the large json response, the _M.body_filter will execute many times. I store every ngx.arg[1] to variable backend_content, but the ngx.arg[1] not clear, so ,the final result is repeated.
> solution:
>
> ```lua
> if not isFinished then
> backend_content = backend_content..ngx.arg[1]
> ngx.arg[1] = nil -- have to clear ngx.arg[1]
> end
> ```
Got it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] sniper-xx commented on issue #4074: request help: How to rewrite the data of backend server response?
Posted by GitBox <gi...@apache.org>.
sniper-xx commented on issue #4074:
URL: https://github.com/apache/apisix/issues/4074#issuecomment-821970382
@Firstsawyou I refer to this plugin to write a new plugin. there are tow defferences:
1、 The ‘body' which is not static, is from the backend service;
2、The body content is very large. function of body_filter executed many times;
I try to concatenate the chunck of every body_filter function received from backend,but can't not correct serialize to integrated json document.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] sniper-xx edited a comment on issue #4074: request help: How to rewrite the data of backend server response?
Posted by GitBox <gi...@apache.org>.
sniper-xx edited a comment on issue #4074:
URL: https://github.com/apache/apisix/issues/4074#issuecomment-822111340
@Firstsawyou The problem has resovled. the reason is:
for the large json response, the _M.body_filter will execute many times. I store every ngx.arg[1] to variable backend_content, but the ngx.arg[1] not clear, so ,the final result is repeated.
solution:
```lua
if not isFinished then
backend_content = backend_content..ngx.arg[1]
ngx.arg[1] = nil -- have to clear ngx.arg[1]
end
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org