Denied5
Denied5

Reputation: 125

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.

Upvotes: 0

Views: 53

Answers (2)

Jeroen Appel
Jeroen Appel

Reputation: 9

Perfect answer Denied5! This really saved my day.

To add a small note fore everyone running into the same issue:

  • The mapping template should be for application/json, as AWS defaults to this template.
  • If any custom query mappings were set in the Integration Request; these should be removed. Otherwise, the parameters will be mapped to both the URL and the body. This will cause an error.

Upvotes: 0

Denied5
Denied5

Reputation: 125

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

Upvotes: 0

Related Questions