Ben McNiel
Ben McNiel

Reputation: 8799

Rewrite destination path in api gateway integration request

Say I have a resource like: /foo/{bar} in API Gateway. I want to transform the request path to /bing/baz/{bar} via an integration request template.

It is straight forward to set 'bar' into the request body via:

{ "bar": "$inputs.params('bar')" }

How do I rewrite the destination path at request time?

The solution is hinted at in 'Example Request Response' here:

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

but the docs don't outline exactly how 'With input template:' functions.

Resource: /things/{id}

With input template:
{
    "id" : "$input.params('id')",
    "count" : "$input.path(‘$.things').size()",
    "things" : $input.json(‘$.things')
}

Upvotes: 10

Views: 11487

Answers (2)

nouveu
nouveu

Reputation: 496

In case someone just wants to change prefix of endpoint and leave rest of path as is, e.g. /v2/users -> /api/users here's a solution.

Create route like /v2/{my_path+} and in integration just add mapping overwrite:path with value /api/$request.path.my_path. It will affect all requests that starts with /v2/ so it can save a lot of manual work.

Docs: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-parameter-mapping.html

Upvotes: 3

jackko
jackko

Reputation: 7354

You might be looking for mapping template variable '$context.resourcePath' which will give you the resource path on which the request was made.

EDIT:

You can use path parameters in the URI field in the HTTP integration, which allows you to dynamically map parameters or fields in the body to the destination path. The syntax is the same as for resources, so curly brackets around the parameter like "http://myapi.com/foo/bar/{baz}".

Then you'll be able to specify a mapping expression for 'baz'.

Upvotes: 6

Related Questions