Dart - api.route.all()

Register a single handler for all HTTP Methods (GET, POST, PUT, DELETE, PATCH) on the route.

import 'package:nitric_sdk/nitric.dart';
final customersRoute = Nitric.api("public").route("/customers");
customersRoute.all((ctx) async {
// construct a response for all incoming HTTP requests
final responseBody = {};
ctx.res.json(responseBody);
return ctx;
});

Parameters

  • Name
    handler
    Required
    Required
    Type
    HttpHandler
    Description

    The middleware service to use as the handler for HTTP requests.

  • Name
    security
    Optional
    Optional
    Type
    List<OidcOptions>
    Description

    Security rules to apply with scopes to the entire API.

Notes

When using the all() method to register a single function as the handler for all HTTP methods, none of the other methods should be defined on that route.

import 'package:nitric_sdk/nitric.dart';
final customersRoute = Nitric.api("public").route("/customers");
customersRoute.all((ctx) async {
// handle all requests
});
// Don't call `get()`, `post()`, etc., they're already handled by `all()`
customersRoute.get((ctx) => {
// this handler won't work
});

Examples

Register a method handler function

import 'package:nitric_sdk/nitric.dart';
final customersRoute = Nitric.api("public").route("/customers");
customersRoute.all((ctx) async {
// construct a response for all incoming HTTP requests
final responseBody = {};
ctx.res.json(responseBody);
return ctx;
});

Access the request body

For methods that include a request body, such as POST and PUT, you can access the body from the ctx.req object.

import 'package:nitric_sdk/nitric.dart';
final customersRoute = Nitric.api("public").route("/customers");
customersRoute.all((ctx) async {
final customerData = ctx.req.json();
// parase, validate and store the request payload if it's available
return ctx;
});
Last updated on Oct 14, 2024