Http class abstract final

Outgoing HTTP requests, like Laravel's Http facade:

final response = await Http.withToken(key)
    .baseUrl('https://api.example.com')
    .timeout(const Duration(seconds: 10))
    .retry(3, delay: const Duration(milliseconds: 200))
    .post('/v1/charges', json: {'amount': 5000});
if (response.successful) {
  final id = response.json('id');
}

In tests, fake answers requests without the network and records them for assertions.

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

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

Static Properties

factory → HttpClientFactory
no setter

Static Methods

acceptJson() → PendingRequest
asForm() → PendingRequest
baseUrl(String url) → PendingRequest
delete(String url, {Object? json, Map<String, Object?>? query}) → Future<ClientResponse>
fake([Map<String, Object> stubs = const {}]) → HttpFake
Answers requests from stubs instead of the network. Keys are URL patterns (* is a wildcard; a scheme is optional, as in Laravel); values are a ClientResponse, a ResponseSequence or a function of the ClientRequest. Without stubs every request answers 200.
get(String url, {Map<String, Object?>? query}) → Future<ClientResponse>
patch(String url, {Object? json, Map<String, String>? form, String? body}) → Future<ClientResponse>
post(String url, {Object? json, Map<String, String>? form, String? body}) → Future<ClientResponse>
preventStrayRequests([bool prevent = true]) → void
Makes unstubbed requests throw while faking.
put(String url, {Object? json, Map<String, String>? form, String? body}) → Future<ClientResponse>
request() → PendingRequest
response([Object? body, int status = 200, Map<String, String> headers = const {}]) → ClientResponse
A stubbed response: maps and lists become JSON.
retry(int times, {Duration delay = const Duration(milliseconds: 100), bool when(ClientResponse? response, Object? error)?}) → PendingRequest
sequence(List<ClientResponse> responses) → ResponseSequence
Responses returned in order for repeated requests to one stub.
timeout(Duration timeout) → PendingRequest
withBasicAuth(String username, String password) → PendingRequest
withHeaders(Map<String, String> headers) → PendingRequest
withToken(String token, {String type = 'Bearer'}) → PendingRequest