Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use values in body sent to gateway and send them as header values to the service #828

Closed
AgentGoldPaw opened this issue Dec 9, 2023 · 3 comments

Comments

@AgentGoldPaw
Copy link

Environment info:

KrakenD version: 2.5.0
System info: docker
Hardware specs: M2 mac with 64gb RAM
Backend technology: N/A
Additional environment information:
Describe what are you trying to do:
trying do what the title says. extract data from the body sent to the gateway, and use in the headers of the request to the backend service.

Your configuration file:

{
"$schema": "https://www.krakend.io/schema/krakend.json",
"version": 3,
"name": "Developer API",
"timeout": "60s",
"extra_config": {
"telemetry/logging": {
"level": "DEBUG",
"prefix": "[KRAKEND]",
"syslog": true,
"stdout": true,
"format": "logstash",
"syslog_facility": "local3"
},
"telemetry/logstash": {
"enabled": true
}
},
"timeout": "3000ms",
"cache_ttl": "300s",
"output_encoding": "json",
"debug_endpoint": true,
"echo_endpoint": true,
"endpoints": [
{
"endpoint": "/publicapi/v1/{route}",
"method": "GET",
"output_encoding": "no-op",
"backend": [
{
"url_pattern": "/publicapi/v1/{route}",
"encoding": "no-op",
"sd": "static",
"method": "GET",
"disable_host_sanitize": false,
"host": [
"https://api.localhost.com"
]
}
]
},
{
"endpoint": "/publicapi/v1/{route}",
"method": "POST",
"output_encoding": "no-op",
"backend": [
{
"url_pattern": "/publicapi/v1/{route}",
"encoding": "no-op",
"sd": "static",
"method": "POST",
"disable_host_sanitize": false,
"host": [
"https://api.localhost.com"
]
}
],
"input_headers": [
"*"
]
},
{
"endpoint": "/oauth2/authorize",
"method": "GET",
"output_encoding": "no-op",
"backend": [
{
"url_pattern": "/",
"encoding": "no-op",
"method": "GET",
"host": [
"https://signin.localhost.com"
]
}
],
"extra_config": {
"modifier/lua-proxy": {
"post": "local r = response.load(); local req = request.load(); print(req:query()); r:statusCode(301); r:headers('location', 'https://signin.localhost.com/authorize?' .. req:query())"
}
}
}
]
}

@AgentGoldPaw
Copy link
Author

it looks like this is only possible with enterprise.... is there a trial period for enterprise so i can do a load test to see if this is something i want?

@alombarte
Copy link
Member

Hi @AgentGoldPaw ,

You can achieve this with the open source edition. Here's an example configuration:

{
    "$schema": "https://www.krakend.io/schema/v2.5/krakend.json",
    "version": 3,
    "name": "My KrakenD API Gateway",
    "port": 8080,
    "host": ["http://localhost:8080"],
    "echo_endpoint": true,
    "endpoints": [
      {
        "endpoint": "/test",
        "method": "POST",
        "backend": [
          {
            "url_pattern": "/__echo/",
            "allow": ["req_headers"],
            "extra_config": {
                "modifier/lua-backend": {
                  "allow_open_libs": true,
                  "pre": "local r = request.load(); r:headers('X-Body1', r:body())"
                }
              }
          }
        ]
      }
    ]
  }

And the output is:

curl -XPOST -d 'hithere' http://localhost:8080/test   
{"req_headers":{"Accept-Encoding":["gzip"],"Content-Type":["application/x-www-form-urlencoded"],"User-Agent":["KrakenD Version 2.5.0"],"X-Body1":["hithere"],"X-Forwarded-For":["172.17.0.1"],"X-Forwarded-Host":["localhost:8080"]}}

If your body is in JSON or another format, you will additionally need to parse the body (because it comes as a string) if you want a specific field, but other than that you can see this is feasible in CE.

If you want to test the EE version you can get in touch with sales@krakend.io

Copy link

This issue was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants