- Newest
- Most votes
- Most comments
Hello Hank,
I encountered similar behaviour on certain occasions in the past. The fact that it works with Accept: application/json but fails with Accept: text/html suggests the issue is probably related to how Django generates HTML responses and how API Gateway processes them.
For me it was always boiling down to following:
- Response Size Limits
In general, HTML responses are usually larger than JSON responses.
I would first do following:
- Compare the size of your HTML responses vs JSON responses
- Look for any large static assets being inline in HTML
- Check if Django Debug Toolbar or similar is adding significant content
- Binary Media Types Configuration
Is your API Gateway configured the right way to handle binary content types. HTML responses might contain characters that need binary encoding. If this is the case, in your API Gateway settings, add these binary media types:
- text/html
- */*(as a fallback)
- Lambda Response Format
Ensure your Lambda function returns responses in the correct format for API Gateway.
Hope it helps
Best regards, Neven
answered a year ago
This issue where API Gateway returns a 503 error when the Accept header is set to text/html but works fine with application/json could be related to content type handling in API Gateway.
Here are some potential causes and solutions to investigate:
-
Content Type Configuration: API Gateway might be configured to handle application/json correctly but not text/html. Check if you have any specific content type mappings or transformations in your API Gateway settings.
-
Binary Content Handling: If your Django application is returning HTML content, API Gateway might need to be configured to handle binary content types. In the API Gateway console, check your API's settings and ensure that "text/html" is added to the binary media types list if you're expecting HTML responses.
-
Response Transformation: Your Lambda function might be returning responses in a format that API Gateway can process correctly for JSON but not for HTML. Ensure your Lambda function is properly formatting responses for both content types.
-
CORS Configuration: If this is a cross-origin request issue, verify that your CORS settings in API Gateway include the appropriate headers for both content types.
-
Lambda Proxy Integration: If you're using Lambda proxy integration, make sure your Lambda function is returning the proper structure with statusCode, headers, and body fields. The headers should include the correct Content-Type based on what was requested.
-
Headers Configuration: Check if you've added specific headers in the Method request or Method response options in API Gateway. Sometimes adding specific content types in these settings can cause unexpected behavior.
To troubleshoot:
-
Check CloudWatch logs for both Lambda and API Gateway to see if there are any specific error messages.
-
Test your API directly from the API Gateway console to isolate if the issue is with the Gateway or your client application.
-
Try simplifying your response to a basic HTML response to see if that works.
-
Verify that your Lambda function is correctly handling different Accept headers and responding appropriately.
Since the issue started after updating one Lambda function but affects others as well, it's possible that there was a change in a shared configuration or dependency that impacts how content types are handled across your API Gateway setup.
Sources
API Gateway calls returning HTML, direct calls returning JSON | AWS re:Post
API gateway runs into preflight issue forever after adding a specific Content-Type | AWS re:Post
Api gatway with Lambda function | AWS re:Post
How Does API Gateway Handle the Response Returned by a Lambda Function? | AWS re:Post
answered a year ago
Relevant content
asked 4 years ago

Thank you so much for your detailed response! Your suggestions are really helpful and I appreciate you sharing your experience.
Do you have any other ideas about what might be causing this issue? I'm particularly puzzled because it affects both updated and non-updated Lambda functions. Any additional troubleshooting steps you'd recommend would be greatly appreciated!
Hello Hank,
thank you for additional clarification. Since this is happening only with "text/html" content type, the problem is highly likely with the encoding of the response. Few additional stuff which one could check/try:
Best regards, Neven
Thank you very much for providing the solution.
Thank you once again for your assistance. I really appreciate it.