An opinionated wrapper around the revered http package, where HTTP responses are objects for easy parsing and error handling.
The easiest way to use this library is via the top-level Connection
class.
import 'package:connection/connection.dart';
void main() async {
/// Initialize the main object
Connection connection = Connection(
authority: 'swapi.dev',
defaultHeaders: {'Content-Type': 'application/json'},
);
try {
/// Call the endpoint
Response response = await connection.get(
endpoint: '/api/people/1',
);
if (response.statusCode == 200) {
/// Do stuff with the response
print(response.statusCode);
print(json.decode(response.body));
} else {
/// Handle the server error
print('Server Error: ${response.statusCode}:');
print(response.body);
}
} catch (e) {
/// Throws an exception if an unknown error occurs
print(e);
}
/// Close the connection
connection.close();
}
Implemented:
- https
- default and custom headers
- GET with query params
- POST with payload
Not implemented:
- everything else
If more features are needed, feel free to open a discussion on github.
Submit an issue on github.
All feedback and suggestions for improvements are welcome:
- Open a discussion on github
- Discuss proposed changes
- Submit a PR (optional)
This package is possible thanks to the people and companies who donate money, services or time to keep the project running.
If you're interested in becoming a Sponsor, Backer or Contributor to expand the project, reach out to me on github.
Or buy me coffee at 0xdir.near
.