Forwarding a Request to an Upstream API
To forward a request to an upstream API ("proxy" a request), you can simply use a proxy-request
action, for example
to delegate the incoming request to httpbin.org.
The following flow shows a more advanced example:
The proxy-request
action lets you set the origin
and modify the headers
of the request. Everything else is set up automatically: The client request body will be forwarded as-is, any headers not intended for upstream will be dropped.
If you need to further modify the request, you'll have to configure a request
action that forwards the incoming request with your modifications upstream. This gives you full control over the upstream request, but there's quite much thinking involved to get this fully right.
The following example illustrates, how such a request could look like. We just modify the url
, copy method
and query
using pair producers and set the request body
by referencing the $body
variable:
Since we are implicitly using the default request ID main
in the above example, the body together with its Content-Type
is automatically set as the response body to be sent to the client.
The set-response-headers
action ensures that the HTTP status code of the upstream response and possibly additional header fields are correctly forwarded to the client.
The forwarded request will only have a body if the incoming request does. The Content-Type
request and response headers are passed automatically with the body, therefore we don't have to copy them explicitly.
If the client and the upstream request URL paths do not share a common prefix, you can easily adjust the path by using stripEndpoint
and addPrefix
:
Assuming the following swagger.yaml
for https://client.example.com/
, https://users.upstream.example.com
for UPSTREAM_ORIGIN
, and /v4
for UPSTREAM_PATH_PREFIX
, a client request for https://client.example.com/api/users/profile
will be forwarded to https://users.upstream.example.com/v4/profile
.
See also
Proxying requests to Upstream APIs (cookbook)
proxy-request
action (reference)request
action (reference)
Last updated