https://github.com/teixeirazeus/hequest
Simple and hackable request for dart.
https://github.com/teixeirazeus/hequest
dart hacktoberfest http
Last synced: 3 months ago
JSON representation
Simple and hackable request for dart.
- Host: GitHub
- URL: https://github.com/teixeirazeus/hequest
- Owner: teixeirazeus
- License: mit
- Created: 2022-09-01T21:23:39.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-05-15T17:30:36.000Z (over 2 years ago)
- Last Synced: 2025-06-10T09:07:01.738Z (8 months ago)
- Topics: dart, hacktoberfest, http
- Language: Dart
- Homepage:
- Size: 45.9 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://www.codacy.com/gh/teixeirazeus/hequest/dashboard?utm_source=github.com\&utm_medium=referral\&utm_content=teixeirazeus/hequest\&utm_campaign=Badge_Grade)[](https://github.com/teixeirazeus/hequest)
Simple and hackable request for dart.
## Installing
1. Add dependencies to `pubspec.yaml`
```yaml
dependencies:
hequest:
git:
url: https://github.com/teixeirazeus/hequest
```
2. Run pub get.
```shell
flutter pub get
```
3. Import package.
```dart
import 'package:hequest/hequest.dart';
```
## Using
Hequest was created with simplicity in mind to handle multiple API requests.
You start by declaring an object of type `Hequest` and passing the `baseUrl` of your api.
```dart
final githubApi = Hequest(baseUrl: 'https://api.github.com');
```
### GET
```dart
final response = await githubApi.get('/users/teixeirazeus');
```
### POST
```dart
final response = await githubApi.post('/users/teixeirazeus', body: {
'name': 'Zeus Teixeira',
});
```
### PUT
```dart
final response = await githubApi.put('/users/teixeirazeus', body: {
'name': 'Zeus Teixeira',
});
```
### DELETE
```dart
final response = await githubApi.delete('/users/teixeirazeus');
```
### JWT
All requests support sending jwt token.
To use to use the `WithToken` postfix and pass the token as a parameter.
```dart
final response = await githubApi.getWithToken('/users/teixeirazeus', token);
```
### Tips
It is highly recommended that you use `try catch` to handle errors. Along with a larger abstraction with a class for each endpoint of your api.
```dart
class GithubApi {
final Hequest _hequest;
GithubApi(this._hequest);
Future getUser(String username) async {
try {
final response = await _hequest.get('/users/$username');
if (response.statusCode == 200) {
return json.decode(response.body);
} else if (response.statusCode == 404) {
throw Exception('User not found');
} else {
throw Exception('Failed to load user');
}
} catch (e) {
throw Exception('Error getting user');
}
}
}
```
```dart
final hequest = Hequest(baseUrl: 'https://api.github.com');
final githubApi = GithubApi(hequest);
final user = await githubApi.getUser('teixeirazeus');
print(user.toString());
```
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.