Different Response Headers for 404 Errors in Cascade API (Dart and Shelf)

54 Views Asked by At

I had asked a question about this before, but I realized it got too long and complex, so I created a very simple case. In the code below I created two separate APIs, ApiA and ApiB, each with only one endpoint returning a 404.

Does anyone know why I only receive the "Response.notFound('body test', headers: {'head-test': 'test'})" in the last API added to the Cascade in the Main? In this example, ApiA is returning a "Response.notFound('Route not found')" without additional headers.

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

void main() async {
  var apiAHandler = ApiA();
  var apiBHandler = ApiB();
  var cascadeHandler = Cascade().add(apiAHandler.router).add(apiBHandler.router).handler;
  await shelf_io.serve(cascadeHandler, 'localhost', 8080);
  print('Server Online');
}

class ApiA {
  Router get router {
    Router router = Router();
    router.get('/apia', (Request req) async {
      return Response.notFound('body test', headers: {'head-test': 'test'});
    });
    return router;
  }
}

class ApiB {
  Router get router {
    Router router = Router();
    router.get('/apib', (Request req) async {
      return Response.notFound('body test', headers: {'head-test': 'test'});
    });
    return router;
  }
}
0

There are 0 best solutions below