cloudevents / sdk-rust

Rust library to interact with CloudEvents
Apache License 2.0
177 stars 61 forks source link

How to send/emit cloud events #210

Open oasisk opened 1 year ago

oasisk commented 1 year ago

Javascript sdk supports sending events , how to send events using rust sdk.

https://github.com/cloudevents/sdk-javascript#emitting-events

Lazzaretti commented 1 year ago

Hi @oasisk

The SDK supports sending events. Is your question how to send an event to an HTTP endpoint? Are you looking for a browser-based solution (so with a WebAssembly)? And how should the event be encoded (e.g. JSON)?

oasisk commented 1 year ago

@Lazzaretti ..yes..want to understand how do we send events to http end point?

ozabalaferrera commented 2 weeks ago

This thread is very old, and I presume moot, but here is a simple example: maybe we could close this issue?

// [dependencies]
// cloudevents-sdk = {version = "0.7", features = ["reqwest"]}
// reqwest = "0.11"
// tokio = {version = "1.41", features = ["rt-multi-thread", "macros"]}

use cloudevents::{binding::reqwest::RequestBuilderExt, Data, EventBuilder, EventBuilderV10};

#[tokio::main]
async fn main() {
    let ce = EventBuilderV10::new()
        .id("id-1")
        .source("source")
        .ty("type")
        .data("text/plain", Data::String("data".to_owned()))
        .build()
        .unwrap();

    let url = "https://httpbin.org/post";
    reqwest::Client::new()
        .post(url)
        .event(ce)
        .unwrap()
        .send()
        .await
        .unwrap();
}