diff --git a/plugins/zenoh-plugin-rest/src/lib.rs b/plugins/zenoh-plugin-rest/src/lib.rs index b544b3899f..6cea5f8e0c 100644 --- a/plugins/zenoh-plugin-rest/src/lib.rs +++ b/plugins/zenoh-plugin-rest/src/lib.rs @@ -296,7 +296,7 @@ fn with_extended_string R>( result } -async fn query(req: Request<(Arc, String)>) -> tide::Result { +async fn query(mut req: Request<(Arc, String)>) -> tide::Result { log::trace!("Incoming GET request: {:?}", req); let first_accept = match req.header("accept") { @@ -374,6 +374,7 @@ async fn query(req: Request<(Arc, String)>) -> tide::Result { }, )) } else { + let body = req.body_bytes().await.unwrap_or_default(); let url = req.url(); let key_expr = match path_to_key_expr(url.path(), &req.state().1) { Ok(ke) => ke, @@ -397,14 +398,15 @@ async fn query(req: Request<(Arc, String)>) -> tide::Result { QueryConsolidation::from(zenoh::query::ConsolidationMode::Latest) }; let raw = selector.decode().any(|(k, _)| k.as_ref() == RAW_KEY); - match req - .state() - .0 - .get(&selector) - .consolidation(consolidation) - .res() - .await - { + let mut query = req.state().0.get(&selector).consolidation(consolidation); + if !body.is_empty() { + let encoding: Encoding = req + .content_type() + .map(|m| m.to_string().into()) + .unwrap_or_default(); + query = query.with_value(Value::from(body).encoding(encoding)); + } + match query.res().await { Ok(receiver) => { if raw { Ok(to_raw_response(receiver).await) @@ -439,7 +441,7 @@ async fn write(mut req: Request<(Arc, String)>) -> tide::Result ZResult<()> { app.with( tide::security::CorsMiddleware::new() .allow_methods( - "GET, PUT, PATCH, DELETE" + "GET, POST, PUT, PATCH, DELETE" .parse::() .unwrap(), ) @@ -489,8 +491,18 @@ pub async fn run(runtime: Runtime, conf: Config) -> ZResult<()> { .allow_credentials(false), ); - app.at("/").get(query).put(write).patch(write).delete(write); - app.at("*").get(query).put(write).patch(write).delete(write); + app.at("/") + .get(query) + .post(query) + .put(write) + .patch(write) + .delete(write); + app.at("*") + .get(query) + .post(query) + .put(write) + .patch(write) + .delete(write); if let Err(e) = app.listen(conf.http_port).await { log::error!("Unable to start http server for REST: {:?}", e);