我有一个API端点,它连接到这个用Groovy编写的Lambda函数,该函数使用multipart/form-data
并返回CSV或XLSX文件。
我启动并运行了Lambda函数,它返回CSV/XLSX文件作为base 64字符串。
然而,在投入任何更多的应用程序上的工作,我希望能够看到该文件从 Postman 请求下载。
你的Function返回什么?
一个响应,其主体是要返回的文件的base-64编码。
Response Header是什么样的?
就像这样:
Date: Tue, 17 Oct 2023 03:49:15 GMT
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Content-Length: 31680
Connection: keep-alive
x-amzn-RequestId: 7aeba6af-b8e7-4e01-93b2-c7c01bf803ac
Content-Disposition: attachment; filename=Returned Postcards 0ct 4 2023.XLSX
x-amz-apigw-id: M7WXME3EiYcFyrw=
X-Amzn-Trace-Id: Root=1-652e042d-069a3c126955e0a648434148;Sampled=0;lineage=bad4f2c2:0
字符串
请注意,我们在代码中定义了响应头:
Map<String, String> createResponseHeaders(String outputFileName) {
return [
('Content-Type') : this.getContentTypeHeader(outputFileName),
('Content-Disposition') : "attachment; filename=${outputFileName}".toString(),
]
}
private String getContentTypeHeader(String outputFileName) {
final String fileExtension = FileUtils.GetFileExtension(outputFileName).toLowerCase();
switch (fileExtension) {
case "xlsx":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
case "csv":
return "text/csv";
}
throw new IllegalArgumentException("Content type for extension '${fileExtension}' not yet implemented");
}
型
同样,让我们看看返回响应的代码.
当然...最后会变成这样...
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withHeaders(this.createResponseHeaders(outputFile.getName()))
.withIsBase64Encoded(true)
.withBody(Base64.getEncoder()
.encodeToString(outputFile.bytes))
型
当你Send and Download??
当我发送和下载时,我得到一个包含原始base-64输出的.txt文件。我没有看到我请求的XLSX文件。
您的API网关设置是什么样的?
这一点:
x1c 0d1x的数据
Postman请求头是什么样子的?
只是 Postman 生成的默认值:
Content-Type: multipart/form-data; boundary=<calculated when request is sent>
...
User-Agent: PostmanRuntime/7.33.0
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
型
我如何得到这样的响应导致文件实际下载?!
1条答案
按热度按时间ohfgkhjo1#
我能够解决这个问题!
我所做的
在API网关页面上,对于我的端点,我执行以下操作:
调整HTTP请求头
Content-Type
个在控制台中,如果你尝试这样做,它应该看起来像这样:
x1c 0d1x的数据
调整方法响应
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
(空型号)text/csv
(空型号)它应该看起来像这样在您的控制台上,如果你正在尝试:
的
在Postman方面,我只是将所需的
Content-Type
(在本例中为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
)分配给Accept
头。稍微调整一下我的源代码
我在我的
createResponseHeaders()
方法中犯了一个错字。我修复了它:字符串
然后我做发送和下载.它工作!