worker-rs: http hello world

use worker::*;

#[event(fetch)]
async fn fetch(
_req: HttpRequest,
_env: Env,
_ctx: Context,
) -> Result<HttpResponse> {
console_error_panic_hook::set_once();
Ok(http::Response::builder()
.status(http::StatusCode::OK)
.body(Body::empty())?)
}
use worker::*;

#[event(fetch)]
async fn fetch(
_req: HttpRequest,
_env: Env,
_ctx: Context,
) -> Result<HttpResponse> {
console_error_panic_hook::set_once();
Ok(http::Response::builder()
.status(http::StatusCode::OK)
.body(Body::empty())?)
}
How can I put a simple string into the body? Tried many ways but didn't work. The body seems to be only accepting a stream. (http feature flag enabled for worker-rs)
2 Replies
Chihwei
Chihwei2w ago
Thanks! Most of those examples are not with http feature flag turned on, which they are using older Response defined in worker-rs, rather than more widely used rust http crate. I later found I could change the wrapped type of Result to assign it a string, like Result<String>. I am a beginner of Rust, so let me know if I was wrong on anything.