3 unstable releases

0.2.1 Feb 18, 2024
0.2.0 Feb 17, 2024
0.1.0 Feb 12, 2024

#925 in Game dev

Download history 14/week @ 2024-02-08 269/week @ 2024-02-15 52/week @ 2024-02-22 15/week @ 2024-02-29 4/week @ 2024-03-07 2/week @ 2024-03-14 53/week @ 2024-03-28 23/week @ 2024-04-04

57 downloads per month

MIT/Apache

37KB
290 lines

bevy_ehttp

Crates.io Documentation MIT/Apache 2.0

A ehttp Bevy Plugin that works both on native and on WASM.

Simple request will invoke RequestCompleted(pub Result<Response, String>) event once completed.

There is also option to call typed request that will allow to deserialize response to given type by using RequestBundle<T>. More details available in typed.rs example.

Example

use bevy::{prelude::*, time::common_conditions::on_timer};
use bevy_ehttp::prelude::*;

fn main() {
    App::new()
        .add_plugins((MinimalPlugins, HttpPlugin))
        .add_systems(Update, handle_response)
        .add_systems(
            Update,
            send_request.run_if(on_timer(std::time::Duration::from_secs(1))),
        )
        .run()
}

fn send_request(mut commands: Commands) {
    let req = ehttp::Request::get("https://api.ipify.org?format=json");
    commands.spawn(HttpRequest(req));
}

fn handle_response(mut requests: EventReader<RequestCompleted>) {
    for request in &mut requests.read() {
        match &**request {
            Ok(response) => println!("response: {:?}", response.text()),
            Err(e) => println!("response error: {:?}", e),
        }
    }
}

Thanks

Big thanks to the creators of the Bevy Engine and to the foxzool user for creating bevy_http_client that this plugin is based on.

License

bevy_ehttp is dual-licensed under MIT and Apache 2.0 at your option.

Bevy compatibility table

Bevy version Crate version
0.13 0.2
0.12 0.1

Dependencies

~39–79MB
~1M SLoC