I am trying to write a struct from a request payload to kafka, to do it I need to obtain the JSON string representing the struct. Currently, I am loading the object from the request using a struct that implements the Deserialize
and Serialize
traits from rocket::serde::json
.
The code looks like this:
#[macro_use]
extern crate rocket;
use rocket::serde::{Serialize, Deserialize};
use rocket::serde::json::Json;
use rdkafka::config::ClientConfig;
use rdkafka::producer::{BaseRecord, FutureProducer};
#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
struct Credential {
metadata: String,
data: String,
}
#[post("/", data="<c>")]
async fn insert_credentials(c: Json<Credential>) -> &'static str {
// Do stuff with c
let producer: &FutureProducer = &ClientConfig::new()
.set("bootstrap.servers", "kafka:9200")
.set("message.timeout.ms", "5000")
.create()
.expect("Producer creation error");
let delivery_status = producer
.send(
FutureRecord::to("credentials_ingestion")
.payload(Json(c).to_string())
.key("MyKey")
)
.await;
"Ok"
}
But I cannot get the .to_string()
to work. I cannot find anything in the Rocket documentation on how to get the JSON string representation for the struct.