我将grpc端点定义如下。grpc端点返回一个.zip文件。它在grpc通道上工作正常,但在REST端点上下载它时遇到了问题。
我使用envoy来做http代码转换。
我现在的问题是,在REST端点上,下载响应头总是设置为application/json,而不是application/zip,这样zip下载才能正常工作。
你知道如何在转码过程中指示特使设置合适的头部,这样REST下载才能正常工作吗?
// Download build
//
// Download build
rpc DownloadBuild(DownloadBuildRequest) returns (stream DownloadResponse) {
option (google.api.http) = {
get : "/v4/projects/{projectId}/types/{buildType}/builds/{buildVersion}/.download"
headers: {}
};
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = {
description: "Download build.";
summary: "Download build.";
tags: "Builds";
produces: "application/zip";
responses: {
key: "200"
value: {
description: "Download build";
}
}
responses: {
key: "401"
value: {
description: "Request could not be authorized";
}
}
responses: {
key: "404"
value: {
description: "Build not found";
}
}
responses: {
key: "500"
value: {
description: "Internal server error";
}
}
};
}
发布于 2021-03-15 21:39:02
好了,我已经找到了部分解决问题的方法。基本上,我必须使用google.api.HttpBody作为响应,并在其中设置内容类型。https://github.com/googleapis/googleapis/blob/master/google/api/httpbody.proto
我仍然有一个问题,设置下载的文件名和扩展头。
发布于 2021-03-17 01:34:44
为了设置文件名头,我必须定义一个服务器拦截器,类似于这里提到的:
How to pass data from grpc rpc call to server interceptor in java
private class TrailerCall<ReqT, RespT> extends SimpleForwardingServerCall<ReqT, RespT> {
public TrailerCall(final ServerCall<ReqT, RespT> delegate) {
super(delegate);
}
@Override
public void sendHeaders(Metadata headers) {
headers.merge(RESPONSE_HEADERS_HOLDER_KEY.get());
super.sendHeaders(headers);
}
}
https://stackoverflow.com/questions/66613339
复制相似问题