rhttp 0.0.2 rhttp: ^0.0.2 copied to clipboard
Make HTTP requests using Rust for Flutter developers. It uses FFI to call Rust functions from Dart. On the Rust side, it uses reqwest to make HTTP requests.
rhttp #
Make HTTP requests using Rust for Flutter developers.
About #
The default HTTP client in Dart is part of dart:io
, which lacks configurability and performance compared to other HTTP clients.
This package uses FFI with flutter_rust_bridge to call Rust code. This allows you to use a faster and more efficient HTTP client.
On Rust's side, the reqwest crate is used to make the requests.
Getting Started #
➤ Installation #
- Install Rust via rustup.
- Add
rhttp
topubspec.yaml
:
dependencies:
rhttp: <version>
➤ Initialization #
import 'package:rhttp/rhttp.dart';
void main() async {
await Rhttp.init(); // add this
runApp(MyApp());
}
➤ Usage #
import 'package:rhttp/rhttp.dart';
void main() async {
await Rhttp.init();
// Make a GET request
HttpResponse response = await Rhttp.get('https://example.com');
// Read the response
int statusCode = response.statusCode;
String body = response.body;
}
Configuration #
➤ HTTP methods #
You can make requests using different HTTP methods:
// Pass the method as an argument
await Rhttp.request(method: HttpMethod.post, url: 'https://example.com');
// Use the helper methods
await Rhttp.post('https://example.com');
➤ Query parameters #
You can add query parameters to the URL:
await Rhttp.get('https://example.com', query: {'key': 'value'});
➤ Headers #
You can add headers to the request:
await Rhttp.request(
method: HttpMethod.get,
url: 'https://example.com',
headers: const HttpHeaders.map({
HttpHeaderName.contentType: 'application/json',
}),
);
➤ Body #
You can add a body to the request. There are different types of bodies you can use:
Text
Pass a string to the HttpBody.text
constructor.
// Raw body
await Rhttp.request(
method: HttpMethod.post,
url: 'https://example.com',
body: HttpBody.text('raw body'),
);
JSON
Pass JSON map to the HttpBody.json
constructor.
The Content-Type header will be set to application/json
if not provided.
// JSON body
await Rhttp.request(
method: HttpMethod.post,
url: 'https://example.com',
body: HttpBody.json({'key': 'value'}),
);
Binary
Pass a Uint8List
to the HttpBody.bytes
constructor.
// Binary body
await Rhttp.request(
method: HttpMethod.post,
url: 'https://example.com',
body: HttpBody.bytes(Uint8List.fromList([0, 1, 2])),
);
Form
Pass a flat map to the HttpBody.form
constructor.
The Content-Type header will be set to application/x-www-form-urlencoded
if not provided.
// Form body
await Rhttp.request(
method: HttpMethod.post,
url: 'https://example.com',
body: HttpBody.form({'key': 'value'}),
);
➤ HTTP version #
You can specify the HTTP version to use for the request:
await Rhttp.get('https://example.com', httpVersion: HttpVersionPref.http3);
License #
MIT License
Copyright (c) 2024 Tien Do Nam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.