'How to redirect a request in Dart/shelf?
I'm using shelf in Dart to setup a simple web server. How can I redirect a request from / to /oauth2callback in the example below?
I have found this method, but somehow I cannot compile the code even though I import dart:io.
import 'dart:io';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
final router = Router()
..get('/', rootHandler)
..get('/oauth2callback', oauth2callback);
Response rootHandler(Request req) {
return Response.ok('Hello, World!\n');
}
Response oauth2callback(Request request) {
return Response.ok('Redirected!\n');
}
void main(List<String> args) async {
final _handler = Pipeline().addMiddleware(logRequests()).addHandler(router);
final server = await serve(_handler, '0.0.0.0', 8080);
print('Server listening on port ${server.port}');
}
Solution 1:[1]
You could use standard Self methods:
return Response.movedPermanently(newUrl); // generates 301
or
return Response.found(newUrl); // generates 302
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gad D Lord |
