enhanced_http 1.0.1 enhanced_http: ^1.0.1 copied to clipboard
An enhanced version of the dart http package based on an axios like structure
enhanced-http #
An enhanced version of the dart http package based on axios
Getting started #
Initialize enhanced http as follows :
EnhancedHttp.initialize(baseURL: 'https://dog.ceo/api');
Custom headers can be provided through the name parameter 'headers' ( Default content type is specified as application/json ) :
EnhancedHttp.initialize(
baseURL: Constants.apiBaseURL,
headers: {'Authorization': "Bearer $token"},
);
A default error message can be provided to return in case of unexpected errors :
EnhancedHttp.initialize(
baseURL: Constants.apiBaseURL,
defaultErrorMessage: "An error has occurred please try again later"
);
Provide a function which can isolate and return an error from the response format of your server :
convertAndNotifyError(dynamic errorResponse){
if (errorResponse['error'].runtimeType == String)
return errorResponse['error'];
if (errorResponse['error'].runtimeType == [].runtimeType){
String firstKey = '';
errorResponse['error'].forEach((key, value) {
if (firstKey == '') firstKey = key;
});
return errorResponse['message'][firstKey]['error'];
}
return "An error has occurred please try again later";
}
EnhancedHttp.initialize(
baseURL: Constants.apiBaseURL,
errorTransformer: convertAndNotifyError
);
Usage #
Fetch data from an api endpoint - GET
final res = await EnhancedHttp.get(path: "/path", successStatusCode: 200);
Send data to an api endpoint - POST
final res = await EnhancedHttp.post(path: "/path", successStatusCode: 200, {
"data": "This is some sample data to send to a server"
});
Update data at an api endpoint - PUT
final res = await EnhancedHttp.put(path: "/path", successStatusCode: 200, {
"data": "This is some sample data to update at a server"
});
Delete data at an api endpoint - DELETE
final res = await EnhancedHttp.delete(path: "/path", successStatusCode: 200);
Additional Parameters #
All requests can take in a function which executes if the response status code matches the provided successStatusCode
final res = await EnhancedHttp.get(path: "/path", successStatusCode: 200, onSuccess: () => {
print("Data fetched from server successfully");
});
Specify the 'formData' attribute as true if the request payload needs to be in the form of multipart/form-data
final res = await EnhancedHttp.post(path: "/path", successStatusCode: 200,
payload: {
"data": "This is some sample data to update at a server"
},
formData: true,
onSuccess: () => {
print("Multipart request sent successfully");
}
);
The files attribute on the http post and http put methods can be used to send files to the server
final res = await EnhancedHttp.post(path: "/path", successStatusCode: 200,
payload: {
"data": "This is some sample data to update at a server"
},
formData: true,
files: [
{
"array_key": "file",
"file": File("path_to_file")
}
],
onSuccess: () => {
print("File sent successfully");
}
);
Credits #
Based on the original dart http package by Google (https://pub.dev/packages/http)