I am constantly getting the error "HTTP headers are not mutable" in Dart.
What I am trying to do is when a user asks for something in the "packages" folder, it will fetch it.
My code with the error is as such:
if(new RegExp("/packages/(.*)").hasMatch(request.uri.toString())){
new File(request.uri.toString().substring(1)).readAsString().then((String contents){
request.response.headers.contentType = new ContentType("text", "css", charset: "utf-8");
request.response.write(contents);
request.response.close();
});
}
And my entire code is this:
import "dart:io";
void main() {
HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 8000).then((HttpServer server){
print("Listening on localhost on {$server.port}");
server.listen((HttpRequest request){
switch(request.uri.toString()){
case "/":
request.response.headers.contentType = ContentType.HTML;
new File("static/taskbar.html").readAsString().then((String contents){
request.response.write(contents);
request.response.close();
new File("static/index.html").readAsString().then((String contents){
request.response.write(contents);
request.response.close();
});
});
break;
default:
request.response.statusCode = HttpStatus.NOT_FOUND;
request.response.write("404 Not Found.");
request.response.close();
break;
}
if(new RegExp("/packages/(.*)").hasMatch(request.uri.toString())){
new File(request.uri.toString().substring(1)).readAsString().then((String contents){
request.response.headers.contentType = new ContentType("text", "css", charset: "utf-8");
request.response.write(contents);
request.response.close();
});
}
});
});
}
Basically, when you get an "HTTP headers not mutable" error, it means that you are trying to modify the headers after writing to the response.
In your code, when the request matches the /packages/(.*)
pattern, it also executes the default
clause of your switch, which produces a 404 response. So, when you try to write to the response again, the server throws an exception.
To solve this problem, you can replace the switch by an if else
expression. Example:
HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 8000).then((HttpServer server){
print("Listening on localhost on {$server.port}");
server.listen((HttpRequest request){
var uri = request.uri.toString();
if (uri == "/") {
request.response.headers.contentType = ContentType.HTML;
new File("static/taskbar.html").readAsString().then((String contents){
request.response.write(contents);
request.response.close();
new File("static/index.html").readAsString().then((String contents){
request.response.write(contents);
request.response.close();
});
});
} else if(new RegExp("/packages/(.*)").hasMatch(uri)){
new File(uri.substring(1)).readAsString().then((String contents){
request.response.headers.contentType = new ContentType("text", "css", charset: "utf-8");
request.response.write(contents);
request.response.close();
});
} else {
request.response.statusCode = HttpStatus.NOT_FOUND;
request.response.write("404 Not Found.");
request.response.close();
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With