保持HttpRequest流打开,而我发出Client.post



我的dart服务器有一个接收来自客户机的POST请求的函数。我想使用来自该请求的信息向另一台服务器发出POST,然后使用来自这台服务器的响应向原始客户机请求发送响应。当我向另一个服务器发出POST时,它会关闭入站客户机请求上的流,我无法再响应。是否有一种方法来保持请求流"活着",而我做一个POST?或者,还有别的方法吗?我确实尝试在不同的隔离上发出POST,但这没有帮助,http请求流仍然被关闭。

void postRequest(HttpRequest request) async {
final endPoint = 'https://www.anotherserver.com/information';
final client = Client();
final data = request.headers.value('data'); // Get data from client
final response = client.post(endPoint, body: data); // Send data to other server
// Do stuff with the response from endPoint server
// For simplicity of this example, just send back the response body back to the client
// This write call to the request causes an "Stream Sink Closed" exception
// It appears the POST call to the endPoint server, caused the client request stream
// to get closed.
request.response.write(response.body);
await request.response.flush();
await request.response.close();
}

我没有await到client.post....我需要更好地注意这一点。

最新更新