http
http copied to clipboard
send is not a good middleware function
The documentation for package:http says that send is the place to override if you want to intercept and modify requests. It advertises it as a middleware solution, and gives an example at the bottom of the README.
The problem is that the example only intercepts the request, and not the response. To be able to use the response in middleware, say, to read the content-type and decode the body according to what you get, you have to read the stream, but also return a StreamedResponse. But, the StreamedResponse only allows one listener, and that listener is taken up by the _sendUnstreamed function which all the other functions call. So, to do this using the current interface, you would have to convert the stream to a broadcast stream in a middleware, but know that only one middleware is doing that since you can't create multiple broadcasters on a single stream. Quite ugly.
The whole interface doesn't really make sense, since everything is being first funneled to _sendUnstreamed, but we are required to override something that returns a StreamedResponse. If we are already funneling everthing through something that waits for the stream to complete, that is the place we should be overriding.
So, basically I am asking for _sendUnstreamed to be made public and overridable, and that function is really what middleware should be wrapping.
Any news on this? Has it been considered?
Wanted to write an issue exactly for this! Please make sendUnstreamed Public so we can properly intercept responses. This is a much needed feature for adding crashlytics to an flutter app.