jsonamazon-web-servicesrestaws-lambda

AWS API Gateway returning 400 Bad Request when query parameter is encoded JSON


I have a REST API setup in API Gateway that integrates with a Lambda to route incoming requests to another external REST API. It is essentially a passthrough function from our API to another legacy API. The external API allows for a query parameter with a JSON value and I want my API to accept a JSON value too and pass through to the legacy API but I am getting a 400 Bad Request error. I am using Postman for testing which is set to auto encode. I know it is an encoding issue but not sure where those settings might be. I have Lambda proxy integration turned on for this resource.

So, in Postman if I send this request:

https://test.my.api.com/users?id={"email":"test.user@mail.com"}

It actually transforms/encodes to send this:

https://test.my.api.com/users?id={%22email%22:%22test.user@mail.com%22}

Which works just fine with the legacy API. But my API returns 400.

The only way it works for my API is to use EncodeURIComponent to fully encode the value:

https://test.my.api.com/users?id=%7B%22email%22%3A%22test.user%40mail.com%22%7D

The client should be able to simply switch endpoints to point to my API without changing any of their legacy requests/queries but I am stumped.


Solution

  • AWS API gateway don't support { character in request URL.

    More information can be found here: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html#api-gateway-known-issues-rest-apis

    The plain text pipe character (|) and the curly brace character ({ or }) are not supported for any request URL query string and must be URL-encoded.