Skip to content
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

every route has to be redeclared with the options method to pass the CORS #6

Open
arnaudelub opened this issue Oct 2, 2022 · 6 comments

Comments

@arnaudelub
Copy link

Hey, first of all thanks for this plugin
Is this behavior expected?
Is there anyway to add this feature like for python's Flask CORS:

In the simplest case, initialize the Flask-Cors extension with default arguments in order to allow CORS for all domains on all routes.

for instance?

@lenniezelk
Copy link
Owner

Kindly provide more info on what you are trying to achieve

@arnaudelub
Copy link
Author

sure, for instance, if you run this code:

import 'dart:io';

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf_cors_headers/shelf_cors_headers.dart';

// Configure routes.
final _router = Router()
  ..get('/', _rootHandler)
  ..get('/echo/<message>', _echoHandler);

Response _rootHandler(Request req) {
  return Response.ok('Hello, World!\n');
}

Response _echoHandler(Request request) {
  final message = request.params['message'];
  return Response.ok('$message\n');
}

void main(List<String> args) async {
  // Use any available host or container IP (usually `0.0.0.0`).
  final ip = InternetAddress.anyIPv4;

  // Configure a pipeline that logs requests.
  final handler = Pipeline()
      .addMiddleware(corsHeaders())
      .addMiddleware(logRequests())
      .addHandler(_router);

  // For running in containers, we respect the PORT environment variable.
  final port = int.parse(Platform.environment['PORT'] ?? '8080');
  final server = await serve(handler, ip, port);
  print('Server listening on port ${server.port}');
}

Then when the browser is trying to call the OPTIONS on the given route, a route not found will be returned.
The be able to make the CORS pass, you have to add into your router:

  ..options('/', _rootHandler)
  ..options('/echo/<message>', _echoHandler);

So that can each routes in your router pass the CORS.

It's not an issue, i was just wondering if it's possible for this middleware to behave like CORS in python's FLASK

@lenniezelk
Copy link
Owner

I think raise this with the shelf router team, this seems like a routing issue

@SittiphanSittisak
Copy link

Any update?
How do I solve the preflight at this moment?
@arnaudelub I follow you with

  ..options('/echo/<message>', _echoHandler);

But it doesn't work the post request doesn't happen.

@arnaudelub
Copy link
Author

If you want any help, you will probably have to give more info, code sample, logs, d'art and plugin version, etc....

@SittiphanSittisak
Copy link

SittiphanSittisak commented May 12, 2023

When the Flutter app tried to connect the dart server with the POST method, it will send a request with the OPTIONS method to the dart server and I need a solution to handle this request.
I fix it by using

final _router = shelf_router.Router()
  //other route
  ..all('/<ignored|.*>', (Request request) {
    if (request.method == 'OPTIONS') return Response.ok(null, headers: myHeader);
    return Response.internalServerError();
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants