Future based Http request package for the Dart and browser. It is inspired by axios
package in Node.js
Install
# recommened install
dart pub add loreq
Usage
Import loreq
in your file.
import 'package:loreq/loreq.dart';
void main() {
loreq('https://example.com/cars?brand=bmw')
.then((data) => print(data));
}
And now we got the bmw
brands from /cars
on our server and printed them on the terminal. How simple isn't it?
import 'package:loreq/loreq.dart';
void main() {
loreq('https://example.com/cars', params: {'brand': 'bmw'})
.then((data) => print(data));
}
Post, Put and Delete requests
When using the loreq
function, the get
request is automatically thrown if no method is specified. However, you can use other request types if you want.
Post
import 'package:loreq/loreq.dart';
void main() {
loreq('https://example.com/users/new', params: {'id': 12345, 'username': 'JohnDoe'} method: 'POST')
.then((data) => print(data));
}
Put
import 'package:loreq/loreq.dart';
void main() {
loreq('https://example.com/password/change', params: {'id': 12345, 'newPassword': 'himom'} method: 'PUT')
.then((data) => print(data));
}
Delete
import 'package:loreq/loreq.dart';
void main() {
loreq('https://example.com/users/delete', params: {'id': 12345, 'password': 'himom'} method: 'DELETE')
.then((data) => print(data));
}
Browser
If you want to use Loreq in normal Javascript, you don't need to convert your Dart code to Javascript code. Loreq
already comes with a file named browser.js
.
// ES6+
import loreq from 'packages/loreq/web/browser.mjs'
// CommonJS (for Node.js)
const loreq = require('packages/loreq/web/node.js')
loreq('https://example.com/packages?id=loreq')
.then(data => console.log(data))
You can also use post
, put
and delete
requests as you would in Dart.
// ES6+
import loreq from 'packages/loreq/web/browser.mjs'
// CommonJS (for Node.js)
const loreq = require('packages/loreq/web/node.js')
loreq('https://example.com/packages/new', {
params: { name: 'loreq', creatorToken: /* ... */},
method: 'POST'
}).then(data => console.log(data))
Errors
You can access data['error']
to see if the response after a request returns an error.
import 'package:loreq/loreq.dart';
void main() {
loreq('https://example.com/packages/aPackageForFake') // An incorrect request
.then((data) {
if (data['error']) {
print('Something went wrong')
} else {
// ...
}
})
}
Example
See example for see usage example
Changelog
See changelog for see changelog