有没有办法设置mimeType
of HttpClientRequest
?
我正在尝试做一些单元测试,在其中创建一个实例,HttpClient
然后向 a 发送一个带有数据字节的发布请求,HttpServer
然后根据mimeType
I 设置对其进行解析。像这样的东西:
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();
});
}
所以HttpHeaders
是不可变的。任何解决方法?