I've set up AWS API Gateway to pass through requests to a service that returns images.
When I use the "Test" functionality in the UI, the logs show the PNG data being returned in the method response, as well as the `Content-Type=image/png:
However, when you actually go and visit the endpoint in a browser, the Content-Type
is application/json
.
I would have expected that the "Method response headers" displayed in the logs of the "Test" UI to match what would actually be returned.
How do I force API Gateway to return the upstream's Content-Type (image/png
in this case, but others more generally) to the browser?
Here is the endpoint as defined in the Swagger 2.0 syntax:
"/format/{id}/image.png": {
"get": {
"tags": [],
"summary": "",
"deprecated": true,
"operationId": "get-png",
"produces": [
"image/png"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "My Description",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Successful operation",
"schema": {
"type": "file"
},
"headers": {
"Access-Control-Allow-Origin": {
"type": "string",
"description": "URI that may access the resource"
},
"Content-Type": {
"type": "string",
"description": "Response MIME type"
}
}
}
},
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Access-Control-Allow-Origin": "'*'",
"method.response.header.Content-Type": "integration.response.header.Content-Type"
}
}
},
"requestParameters": {
"integration.request.path.id": "method.request.path.id"
},
"uri": "https://[image_service]/{id}.png",
"passthroughBehavior": "when_no_match",
"httpMethod": "GET",
"type": "http"
}
}
}
Notes:
- This endpoint is somewhat simplified (but still illustrates the problem). However in reality, there is more to the endpoint (ie. I'm not just proxying the requests, but also rewriting paths + query params).
- As noted in this answer, if your endpoint is just proxing requests to an image server, you should probably use AWS CloudFront instead. It has edge caching included in its price, and is ~3x cheaper.