Search code examples
amazon-web-servicesaws-api-gatewayamazon-cloudsearch

Issue with slashes for Integrating API Gateway with CloudSearch


I am currently facing an issue while trying to integrate AWS API Gateway with CloudSearch. I have followed the official documentation to perform the integration.

The problem arises when my backend service sends encoded requests to the API Gateway, which are then decoded before being sent to CloudSearch.

Here's an example:
Backend service sends: pretty%3Dtrue%26fq%3Dsectors%3A%27One%2FTwo%27%26q%3Dmatchall%26q.parser%3Dstructured
API Gateway decodes it to: pretty=true&fq=sectors:'One/Two'&q=matchall&q.parser=structured
Then, it sends to CloudSearch: q=matchall&pretty=true&q.parser=structured&fq=sectors:'One/two'

Unfortunately, the slash in the request is no longer encoded when it reaches CloudSearch, which results in a 404 error.

I have also found a similar unresolved thread here: link

I would appreciate any insights or suggestions on how to resolve this issue. Is there a way to force API Gateway not to decode parameters, or to make CloudSearch expect decoded parameters?

Thank you in advance.


Solution

  • You need to change your API Gateway method to POST. And apply the script to move the search query to the body.

    #set($context.requestOverride.header.Content-Type = 'application/x-www-form-urlencoded')
    #set($allParams = $input.params().querystring)
    #set($queryString = "")
    #foreach($paramName in $allParams.keySet())
    #set($queryString = $queryString + "$paramName=$util.urlEncode($allParams.get($paramName))&")
    #end
    $queryString