cloudevents / sdk-rust

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

application/json content type set with reqwest integration insetad of expected application/cloudevents+json #214

Open tizz98 opened 1 year ago

tizz98 commented 1 year ago

I'm using the reqwest integration for sending cloud events, but the content type seems to always be set to application/json instead of application/cloudevents+json.

Here's the request made to my pipedream request bin:

Screenshot

Cargo.toml

[package]
name = "cloudevents-reqwest-demo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.75"
cloudevents-sdk = { version = "0.7.0", features = ["reqwest"] }
reqwest = "0.11.20"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
tokio = { version = "1.32.0", features = ["full"] }
uuid = { version = "1.4.1", features = ["v4"] }

main.rs

use std::collections::HashMap;

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

#[tokio::main]
async fn main() {
    let target = "https://<OMITTED>.m.pipedream.net";
    let client = reqwest::Client::new();
    let mut data = HashMap::new();
    data.insert("hello", "world");

    let event = EventBuilderV10::new()
        .id(uuid::Uuid::new_v4().to_string())
        .ty("example.test")
        .source("http://localhost/")
        .data("application/json", serde_json::to_value(data).unwrap())
        .build()
        .unwrap();

    let resp = client
        .post(target)
        .event(event)
        .unwrap()
        .send()
        .await
        .unwrap();

    println!("Response: {:?}", resp);
}

I've tried a few other things that don't seem to work:

  1. Setting .data("application/cloudevents+json")
  2. Setting a .header() for content type on the request builder
  3. Using json!() serde macro for .data

I followed this example for getting set up, but I'm not using wasm.

gabriel-araujjo commented 6 months ago

That's not a bug. The sdk is using the Binary content mode to delivery the event using HTTP. In this mode, the HTTP Content-Type header corresponds to the CloudEvents datacontenttype attribute.

Source: https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/bindings/http-protocol-binding.md#311-http-content-type