将响应传递给顺序请求(HTTP_GET-> HTTP_POST)

时间:2019-05-12 17:17:29

标签: go api-gateway gin

我有一个微服务,其输出以下内容作为响应。 这是一个HTTP_GET请求,它在http://localhost:8050/location?company=5

上投放
curl -X GET http://localhost:8050/location?company=5
{
    "locations": [
        { "key": "1050", "lat": 24.000000000, "lon": 46.000000000 },
        { "key": "1254", "lat": 24.999999999, "lon": 46.999999999 }
    ]
}

我还有一个HTTP_POST请求,该请求接受键列表并返回relevent分支详细信息。 它在http://localhost:8060/branches上投放并接受json数组。

curl -X POST http://localhost:8060/branches -H 'Content-Type: application/json' -d '["1050","1254"]'

输出将是

{
    "branches": [
        { "key": "1050", "branch_name": "DC", "telephone": "+1 11 111 111" },
        { "key": "1254", "branch_name": "LA", "telephone": "+1 22 222 222" }
    ]
}

我需要如下的最终合并输出

{
    "branches": [
        { "key": "1050", "branch_name": "DC", "telephone": "+1 11 111 111" },
        { "key": "1254", "branch_name": "LA", "telephone": "+1 22 222 222" }
    ],
    "locations": [
        { "key": "1050", "lat": 24.000000000, "lon": 46.000000000 },
        { "key": "1254", "lat": 24.999999999, "lon": 46.999999999 }
    ]
}

我正在使用krakend开发一个api网关,它将执行上述操作并在http://localhost:8080/findBranches?company=2上投放
这是我的krakend-config.json

{
    "version": 2,
    "extra_config": {},
    "timeout": "3000ms",
    "cache_ttl": "300s",
    "output_encoding": "json",
    "name": "sample",
    "endpoints": [
      {
        "endpoint": "/findBranches",
        "method": "GET",
        "extra_config": {
          "github.com/devopsfaith/krakend/proxy": {
            "sequential": true
          }
        },
        "headers_to_pass": [ "*" ],
        "querystring_params": [ "company" ],
        "output_encoding": "json",
        "concurrent_calls": 1,
        "backend": [
          {
            "url_pattern": "/location",
            "encoding": "json",
            "method": "GET",
            "host": [ "http://localhost:8050" ],
            "disable_host_sanitize": true
          },
          {
            "url_pattern": "/branches",
            "encoding": "json",
            "method": "POST",
            "host": [ "http://localhost:8060" ],
            "disable_host_sanitize": false
          }
        ]
      }
    ]
}

如何处理第一个后端的响应并将其发送到第二个后端的发帖请求?

0 个答案:

没有答案