context.headers / context.setHeader()

Access request headers and set response headers.

This API can only be used on the server and is only available using the package:jaspr/server.dart import.

context.headers

With the context.headers and context.headersAll getters you can access the HTTP headers of the currently handled request on the server.

class MyComponent extends StatelessComponent {
  @override
  Iterable<Component> build(BuildContext context) sync* {
    final userAgent = context.headers['user-agent'];
    yield p([text('User-Agent: $userAgent')]);
  }
}

context.setHeader()

The context.setHeader(String name, String value) method lets you set a response header for the current request.

class MyComponent extends StatelessComponent {
  @override
  Iterable<Component> build(BuildContext context) sync* {
    context.setHeader('X-Rendered-By', 'Jaspr');

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