AWS API Gateway: How to remove/replace query string parameter in HTTP Proxy Passthrough integration?

Viewed 925

When I query my Invoke URL as https://xxx.execute-api.yyy.amazonaws.com/test/q?apiKey=AAA with my below setup my backend receives a call as https://api.mysite.com/q?apiKey=AAA&apiKey=111: one apiKey=AAA comes from the client, the second one - apiKey=111 comes from the Integration Request configuration.

Question: What/How should I configure an integration that apiKey=AAA either removed from the client call or replaced on the integration step with 111 value so that only one apiKey comes to the backend?

Note:

  • with proxy passthrough integration the Mapping Templates are not available;
  • the reason for such configuration is that my legacy backend has a big amount of endpoints which is not possible to configure individually.

My setup:

  • I have created a new REST API.
  • Then I have created a new Configure as a Proxy resource named proxy with a Resource Path /{proxy+} with the following setup for ANY method as a proxy integration:
    • Integration type HTTP Proxy
    • Endpoint URL: https://api.mysite.com/{proxy}
    • Content Handling: Passthrough
  • As a next step, I have configured an Integration Request for my /{proxy+} - ANY by adding a new query string to the URL Query String Parameters section:
    • Name: myApiKey
    • Mapped from: '111'
  • Then I click Deploy API to test stage and getting Invoke URL, let's say: https://xxx.execute-api.yyy.amazonaws.com/test.
1 Answers

Even with Proxy Integration, we can still override Request & Response. Here is the blog. Let me try to summarize.

  • Ensure that Use Proxy Integration is unchecked
  • Simple VTL template in Mapping Template to replace apiKey queryParameter.
  #set($newApiKey = "abcd")
  $input.json("$")
  #set($context.requestOverride.querystring.apiKey = $newApiKey)
  • Add Method Responses example response codes 200, 400 and 500.
  • Add Integration Response for each status code for each response codes for example http status for 2xx 2\d{2} with pass through behaviour.

Lets say we have a proxy setup for path /someapi/sompath. Above template will replace /someapi/sompath?apiKey=100 to {proxy}?apiKey=abcd

Related