Tuesday, 10 September 2013

Dart - set mimetype of HttpClientRequest

Dart - set mimetype of HttpClientRequest

Is there some way to set the mimeType of HttpClientRequest ?
I'm trying to do some unittests where I create an instance of HttpClient
and then sent a post request with a bytes of data to a HttpServer and then
parse it based on the mimeType I set. Something like this :
import 'dart:io';
void main() {
HttpServer.bind('localhost', 8080)
.then((HttpServer server) {
server.listen((HttpRequest request) {
String mimeType = request.headers.contentType.mimeType;
if (mimeType == 'application/json') {
// parse bytes of data to json and then to map
} else if (mimeType == 'text/plain') {
// parse bytes of data to string
}
// do something with data
// close response
});
});
HttpClient client = new HttpClient();
client.open('POST', '127.0.0.1', 8080, '/')
.then((HttpClientRequest r) {
r.add('{"val": 5}'.codeUnits);
// already tried both
r.headers.set(HttpHeaders.CONTENT_TYPE, 'application/json'); //
doesn't work
r.headers.contentType = ContentType.parse('application/json'); //
doesn't work
r.close();
});
}
So the HttpHeaders is immutable. Any workaround?

No comments:

Post a Comment