diff --git a/python/echo/function/func.py b/python/echo/function/func.py index 5fd4e7d..821dc9b 100644 --- a/python/echo/function/func.py +++ b/python/echo/function/func.py @@ -18,10 +18,24 @@ def __init__(self): async def handle(self, scope, receive, send): """ Handle all HTTP requests to this Function other than readiness - and liveness probes. Echoes the query string back to the caller.""" + and liveness probes. Echoes the query string on GET or the request + body on POST.""" - query_string = scope.get('query_string', b'').decode('utf-8') - logging.info(f"Echoing query string: {query_string}") + method = scope.get('method', 'GET') + + if method == 'POST': + body = b'' + while True: + message = await receive() + body += message.get('body', b'') + if not message.get('more_body', False): + break + response_body = body + logging.info(f"Echoing request body: {response_body.decode('utf-8')}") + else: + query_string = scope.get('query_string', b'').decode('utf-8') + response_body = query_string.encode() + logging.info(f"Echoing query string: {query_string}") await send({ 'type': 'http.response.start', @@ -32,7 +46,7 @@ async def handle(self, scope, receive, send): }) await send({ 'type': 'http.response.body', - 'body': query_string.encode(), + 'body': response_body, }) def start(self, cfg): diff --git a/rust/echo/src/handler.rs b/rust/echo/src/handler.rs index de82618..816839b 100644 --- a/rust/echo/src/handler.rs +++ b/rust/echo/src/handler.rs @@ -1,9 +1,9 @@ use crate::config::HandlerConfig; -use actix_web::{http::Method, web::Data, HttpRequest, HttpResponse}; +use actix_web::{http::Method, web, web::Data, HttpRequest, HttpResponse}; use log::info; // Implement your function's logic here -pub async fn index(req: HttpRequest, config: Data) -> HttpResponse { +pub async fn index(req: HttpRequest, body: web::Bytes, config: Data) -> HttpResponse { info!("{:#?}", req); if req.method() == Method::GET { // Echo back the query string if present, otherwise return greeting @@ -14,7 +14,8 @@ pub async fn index(req: HttpRequest, config: Data) -> HttpRespons HttpResponse::Ok().body(format!("Hello {}!\n", config.name)) } } else { - HttpResponse::Ok().body(format!("Thanks {}!\n", config.name)) + // Echo back the request body + HttpResponse::Ok().body(body) } } @@ -30,7 +31,7 @@ mod tests { #[actix_rt::test] async fn get() { let req = TestRequest::get().to_http_request(); - let resp = index(req, config()).await; + let resp = index(req, Bytes::new(), config()).await; assert_eq!(resp.status(), http::StatusCode::OK); assert_eq!( &Bytes::from(format!("Hello {}!\n", "world")), @@ -43,7 +44,7 @@ mod tests { let req = TestRequest::get() .uri("/?test=param&message=hello") .to_http_request(); - let resp = index(req, config()).await; + let resp = index(req, Bytes::new(), config()).await; assert_eq!(resp.status(), http::StatusCode::OK); assert_eq!( &Bytes::from("test=param&message=hello"), @@ -53,11 +54,12 @@ mod tests { #[actix_rt::test] async fn post() { + let payload = Bytes::from("{\"message\":\"Hello World\"}"); let req = TestRequest::post().to_http_request(); - let resp = index(req, config()).await; + let resp = index(req, payload.clone(), config()).await; assert!(resp.status().is_success()); assert_eq!( - &Bytes::from(format!("Thanks {}!\n", "world")), + &payload, to_bytes(resp.into_body()).await.unwrap().as_ref() ); }