Skip to main content

502 Bad Gateway Error

The HTTP 502 Bad Gateway server error response code indicates that the server, while functioning as a gateway or proxy, has received an invalid response from the upstream server.

The cause of 502 Bad Gateway error

The occurrence of a 502 error is often related to either:

  • Timeout: APITemplate.io supports either 30 seconds or 120 seconds depending on the region.
  • Exceeding the maximum payload size: APITemplate.io supports either 4 MB or 6 MB for the payload size depending on the region.

Each region has its own specific maximum response size and timeout. Please refer to the APITemplate.io API Reference for more details on the limitations.

FAQ

Q: I'm using an endpoint that supports up to 4 MB payload, so why am I still getting a 502 error even though my PDF is only 866 KB?

The PDF binary is encoded in base64 between our app function and the load balancer/API Gateway. Base64 encoding causes an overhead of 33–37% (33% due to the encoding itself, and up to 4% more due to inserted line breaks). If a file is about 866 KB and you are using an endpoint with a maximum payload size of 4 MB, it is likely to exceed the 4 MB limit after encoding.

Solutions for Bad Gateway error

There are four potential solutions to address the 502 error:

  1. Use export_type: json: If you are using export_type: file and getting a 502 error, consider using export_type: json instead. With export_type: json, the generated PDFs are stored in our CDN rather than directly returned as a response. This approach helps bypass the limitations imposed by response size restrictions.

  2. Async PDF generation: If the PDF generation takes a long time (30 seconds or more), consider using our async API to generate PDFs asynchronously.

  3. Switch to an alternative region: Another option is to switch to an alternative region that supports a higher maximum response size or longer timeout. This can be a suitable choice when the current region's limitations are consistently reached. You can find the limitations of each region in the API Reference.

  4. JSON data payload exceeds the maximum allowed payload size: Use the load_data_from parameter to load your JSON data from an external URL instead of including it directly in the request body.