FetchHttpTransport class

The HttpTransport for code running in a browser (Flutter web, Dart compiled to JavaScript or WebAssembly), built on the browser's fetch through dart:js_interop.

platformHttpTransport() returns one in the browser, so applications rarely create it. The browser stays in charge of the request: it adds cookies and its own headers, applies CORS, and has no connection timeout.

How it treats an HttpRequest:

  • Several values of one header are sent joined with , .
  • Cookies go with requests to the page's own origin only, unless credentials says otherwise (FetchCredentials.include for an API on another origin of the same site).
  • Redirects are followed unless followRedirects is false; then fetch runs with redirect: 'manual' and the browser gives an opaque response with status 0, without its Location.
  • timeout (the whole exchange) and readTimeout (each wait for data) abort the request through an AbortController, and an HttpTransportException with isTimeout is thrown.
  • Non-2xx responses are ordinary responses. Network failures, including a request the browser blocks for CORS, throw an HttpTransportException.
import 'package:spring_web_dart/browser.dart';

// A relative URL goes to the origin that served the web app.
final transport = FetchHttpTransport();
final response = await transport.send(
  HttpRequest(method: HttpMethod.get, url: Uri.parse('/api/books')),
);

Spring (Java): no browser in Java; the closest is a ClientHttpRequestFactory such as JdkClientHttpRequestFactory.

Implemented types

Constructors

FetchHttpTransport({FetchCredentials credentials = FetchCredentials.sameOrigin})
A transport on fetch. The browser decides about connections, proxies and certificates; credentials says whether it sends its cookies, which a server that keeps the login in a session cookie needs when the API has another origin than the page.

Properties

credentials → FetchCredentials
Whether the browser sends cookies with the requests (the credentials option of fetch).
final
hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

close() → void
Releases the transport's connections. Call it when the transport is no longer needed (the Feign starter does when the application stops); do not use the transport afterwards.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
send(HttpRequest request) → Future<HttpResponse>
Sends request and reads the whole response into memory.
override
stream(HttpRequest request) → Future<StreamedHttpResponse>
Sends request and completes as soon as the status and headers have arrived. The body is then read from StreamedHttpResponse.body as it comes.
override
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited