chilcano commented on issue #9818: URL: https://github.com/apache/apisix/issues/9818#issuecomment-1634138905
Thanks @jiangfucheng and @qwzhou89 Considering your ideas I've got this working code. Leave here if someone is interested. ```sh curl -i http://127.0.0.1:9180/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "methods":["GET"], "uri":"/api01/*", "plugins":{ "serverless-post-function": { "phase": "access", "functions": [ "return function(conf, ctx) local core = require(\"apisix.core\") local upstream_url_ini = ctx.var.upstream_uri local req_uri_real = ctx.var.real_request_uri local splited_uri = core.utils.split_uri(req_uri_real); local last_uri_part = splited_uri[#splited_uri] local last_uri_part_b64 = ngx.encode_base64(last_uri_part) local chars_to_remove = \"[=]\" local upstream_url_fin = upstream_url_ini .. \"/\" .. last_uri_part_b64 ctx.var.upstream_uri = string.gsub(upstream_url_fin, chars_to_remove, \"\") end" ] }, "proxy-rewrite":{ "uri": "/api02/base64", "host":"echo-api.3scale.net", "headers":{ "X-Apikey":"1234567890", "Content-Type":"application/json" } } }, "upstream": { "type": "roundrobin", "scheme": "https", "nodes": { "echo-api.3scale.net": 1 } } }' ``` And testing it I have this: ```sh curl http://127.0.0.1:9080/api01/abc12345 -s | jq . { "method": "GET", "path": "/api02/base64/YWJjMTIzNDU", "args": "", "body": "", "headers": { "HTTP_VERSION": "HTTP/1.1", "HTTP_HOST": "echo-api.3scale.net", "HTTP_X_REAL_IP": "172.18.0.1", "HTTP_X_FORWARDED_FOR": "172.18.0.1,2.120.146.198", "HTTP_X_FORWARDED_PROTO": "https", "HTTP_X_FORWARDED_HOST": "127.0.0.1", "HTTP_X_FORWARDED_PORT": "9080", "HTTP_USER_AGENT": "curl/8.1.2", "HTTP_ACCEPT": "*/*", "HTTP_X_APIKEY": "1234567890", "CONTENT_TYPE": "application/json", "HTTP_X_ENVOY_EXTERNAL_ADDRESS": "2.120.146.198", "HTTP_X_REQUEST_ID": "fdab3c63-351d-4dc4-b111-894e34ec47f4", "HTTP_X_ENVOY_EXPECTED_RQ_TIMEOUT_MS": "15000" }, "uuid": "06d9ec05-af0e-4847-86e2-4fbc79a9e3ed" } ``` This route will receive calls to `http://local-apisix/api01/abc12345`, split the uri to get `abc12345`, encode it to base64 to get `YWJjMTIzNDU=`, remove padding char `=` and override the `ctx.var.upstream_uri`. Finally the `proxy-rewrite` plugin rewrite the request to call a 3rd party API `http://3rd-party-api/api02/base64/YWJjMTIzNDU`, you will see that in above response ` "path": "/api02/base64/YWJjMTIzNDU",`. Thanks again. Regards. -- 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]
