-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Working with HttpRequest content body is unwieldy. #8834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I was actually thinking about this earlier today, and came up with the following: How about having a class which can do this either as a transformer (for the server) or through a future (usable on both client and server). Something like this - see example usage below. class HttpBody implements StreamTransformer<HttpRequest, HttpRequestBody> { static Future<HttpRequestBody> processRequestBody(HttpRequest request); static Future<HttpResponseBody> processResponseBody(HttpResponse response) // The content type e.g. application/json, application/octet-stream, application/x-www-form-urlencoded, ... // Something better that mimeType, e.g. HttpBody.JSON, HttpBody.BINARY, HttpBody.FORM, HttpBody.TEXT ... // The actual body, type of this depending on type (Map for JSON and FROM, List<int> for BINARY etc. // Map if JSON, null otherwise // List<int> if BINARY, null otherwise // Map if FORM, null otherwise // String if TEXT, null otherwise class HttpRequestBody extends HttpBody { class HttpResponseBody extends HttpBody { HttpServer.bind().then((server) { HttpClient client = new HttpClient(); Added this to the M4 milestone. |
For the record, yes! :) Also, for WebSocketTransformer. Thanks! |
This comment was originally written by @butlermatt Sounds very good. Though perhaps also add a getter to the HttpRequestBody to provide the response (ie HttpResponse get response => request.response;), otherwise we'd need to write something like: body.request.response.addString(replyJson); better to be just: body.response... Otherwise, yes I love the idea. Especially since it would work so well with many of the already existing routing implementations (or at least mine ;). But definitely the key point for me is having the body content available synchronously instead of as a stream of data I have to 'collect'. All the extra functionality (body.type) is just the cherry on top ;) |
The change landed in https://code.google.com/p/dart/source/detail?r=21202 support mime types text/* and application/json. Built-in form support (for both application/x-www-form-urlencoded and multipart/form-data) and configuration will be added. Removed the owner. |
Issue #2488 has been merged into this issue. |
Marked this as blocking #7391. |
Issue #2787 has been merged into this issue. |
Issue #5040 has been merged into this issue. |
With the addition of HttpBodyHandler in dart:io the body handling has become much simpler. Added Fixed label. |
Removed Area-IO label. |
This issue was originally filed by @butlermatt
Currently an HttpRequest (even when not persistent) is a Stream of List<int>. As such to get the entire content body we need to do something like this:
List<int> body = new List<int>();
request.listen(body.addAll, onDone: () {
var str = new String.fromCharCodes(body);
// Now do something with the string of data
});
While there some modifications we can do like transform it to a StringDecoder then convert to a list then join said list of strings into the result.. ultimately that's just as (if not more) unwieldy. And neither option seems very dart-y.
I'm wondering if HttpRequest should provide a helper method or property to provide the 'body' of the HttpRequest. Something similar to:
httpRequest.content.then((List<int> body) {
// Optional since we may not always want strings
var str = new String.fromCharCodes(body);
// now do stuff.
});
The text was updated successfully, but these errors were encountered: