xxoxx commented on issue #10759:
URL: https://github.com/apache/apisix/issues/10759#issuecomment-1882125762

   @shreemaan-abhishek 
   I am attempting to extract the 'jwt' from the JSON structure in the response 
body, and then set it as a specific cookie in the header. The code I have 
written is as follows:
   
   ```
   {
       "plugins": {
           "serverless-post-function": {
               "phase": "body_filter",
               "functions": [
                   "return function(conf, ctx) {
                       local core = require('apisix.core'); 
                       local cjson = require 'cjson.safe' 
                       local buffer = ngx.ctx.buffer 
                           if not buffer then 
                               buffer = {} 
                               ngx.ctx.buffer = buffer 
                           end 
                           local chunk = ngx.arg[1] 
                           local eof = ngx.arg[2] 
                           table.insert(buffer, chunk)
                           if eof then 
                               local response_body = table.concat(buffer) 
                               local decoded_body = cjson.decode(response_body) 
                               if decoded_body then 
                                   local jwt = decoded_body.jwt
                                   ngx.log(ngx.ERR, jwt)
                                   if jwt then 
                                       core.response.set_header('Set-Cookie', 
'jwtcookie=' .. jwt .. '; path=/; domain=; expires=');
                                       ngx.header['Set-Cookie'] = 'jwtcookie=' 
.. jwt .. '; path=/; HttpOnly' 
                                   end
                               end 
                           end 
                       end"
               ]
           }
       }
   }
   ```
   
   I have tried the 'phase' with 'rewrite', 'access', 'header_filter', 
'body_filter', 'log', 'before_proxy', and found from the documentation that 
cookies can be set in the 'header_filter' phase, but it is not possible to get 
the content of the response body in this phase. In the 'body_filter' phase, I 
can get the content of the response body, but I cannot set the cookie in this 
phase. What should I do? Please provide me with a feasible solution
   


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to