context.setStatusCode()

Set the response status code and body.


context.setStatusCode()

The context.setStatusCode() method lets you set the status code for the response.

You can also provide an optional responseBody that will be sent as the response body instead of the rendered html.

dart
class App extends StatelessComponent {
  @override
  Component build(BuildContext context) {
    if (!isAllowedAccess) {
      // Redirect to the signup page.
      context.setHeader('Location', '/signup');
      context.setStatusCode(307, responseBody: 'Need to sign up.');
      return .empty(); // No need to render anything else.
    }

    return p([.text('Hello, World!')]);
  }
}