You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "shreemaan-abhishek (via GitHub)" <gi...@apache.org> on 2023/11/27 02:26:04 UTC

Re: [I] help request: how to add field of $request_body to new header using proxy-rewrite [apisix]

shreemaan-abhishek commented on issue #10548:
URL: https://github.com/apache/apisix/issues/10548#issuecomment-1827030374

   you need to use the serverless plugin first to register the request body to a variable using `core.request.get_body()` and then use it to set a header in `proxy-rewrite` plugin. I am sharing the following examples for you to get started:
   
   ```shell
   curl -XPUT 'http://127.0.0.1:9180/apisix/admin/routes/1' \
   --header 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
   --header 'Content-Type: application/json' \
   --data-raw '{
       "uri": "/transform",
       "plugins": {
           "serverless-pre-function":{
               "phase": "access",
               "functions": ["return function() local core = require(\"apisix.core\"); local body = core.request.get_body(); core.response.set_header(\"X-TEST\", \"TEXT\"); core.response.exit(200, string.upper(body)); end"]
           }
       }
   }'
   ```
   https://the-asf.slack.com/archives/CUC5MN17A/p1663553478727759?thread_ts=1663152493.333589&cid=CUC5MN17A
   
   ```shell
   curl http://127.0.0.1:9180/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
   {
       "uri": "/test",
       "plugins": {
           "serverless-pre-function": {
               "phase": "rewrite",
               "functions" : ["return function() require(\"apisix.core\").ctx.register_var(\"lol\", function(ctx) return string.gsub(ngx.var.request_uri, \"?.*\", \"\"); end)
   ; end"]
           },
           "response-rewrite": {
               "headers": {
                   "set": {
                       "X-Lol": "$lol" <<<< using the variable
                   }
               }
           }
       },
       "upstream": {
           "type": "roundrobin",
           "nodes": {
               "127.0.0.1:1980": 1
           }
       }
   }'
   ```
   https://the-asf.slack.com/archives/CUC5MN17A/p1689745385755319?thread_ts=1689648232.277709&cid=CUC5MN17A


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org