2
votes

We have an API endpoint we are building through API Gateway that can have two possible successful responses: 200 OK if all of the requested data is returned, and a 206 Partial Content if the data is paginated and additional requests are required to fetch all the data.

I found at https://aws.amazon.com/blogs/compute/amazon-api-gateway-mapping-improvements/ that Amazon now allows multiple 2XX responses to be defined, but I cannot attribute both responses to a success in the Integration Response configuration. Right now we have 200 set as the default, but it appears as if we have to specify a Lambda Error Regex for the 206 mapping.

Does this basically mean that I have to fail the 206 with an error message and then use the regex to determine if that message is being sent, and then basically just treat it like a success? Or how can I properly return either a 200 or a 206 as a successful response?

The endpoint that AWS will hit on our server will properly return a 200 or a 206, but when AWS responds to the client it is currently only sending a 200.

2
This seems like a misuse of 206 Partial Content. HTTP status 206 is for responses to Range requests. tools.ietf.org/html/rfc7233#page-10Michael - sqlbot
206 aside, I find it way easier to avoid using the legacy API gateway integration and just do Lambda-Proxy. That way you avoid all this awful mapping stuff and you can use regular HTTP libraries to manage the request-response lifecycle. Shameless plug: github.com/dougmoscrop/serverless-httpDoug Moscrop

2 Answers

0
votes

Does this basically mean that I have to fail the 206 with an error message and then use the regex to determine if that message is being sent, and then basically just treat it like a success?

Yes, that is correct. Quirky, wrong, but it works.

0
votes

The API gateway doesnt provide sending multiple success 2xx response. Below are some options

  1. Make your lambda or backend application fail and return some json response. Create a regex in integration response and map the response to a particular error code. The matching of regex is done on errorMessage field which can be only get once you throw error from lambda.

  2. Utilise reponse overriding. You can create a default mapping which matches all or some responses and then in the mapping template you can override the status code to whatever you want to send back.

Eg:-
Lambda returns

def handler(event, context):
   x = {
     "message": "partial"
   }
   return x

API gateway integration response

Lambda error regex: -
Method response status: 200
Default mapping: Yes

Mapping template:

#set($inputRoot = $input.path('$'))
$input.json("$")
#if($inputRoot.toString().contains("partial"))
    #set($context.responseOverride.status = 206)
#end