Return JSON directly from a Worker script, useful for building APIs and middleware.
export default { async fetch(request) { const data = { hello: "world", }; return Response.json(data); },};
export default { async fetch(request): Promise<Response> { const data = { hello: "world", }; return Response.json(data); },} satisfies ExportedHandler;
from js import Response, Headersimport json def on_fetch(request): data = json.dumps({"hello": "world"}) headers = Headers.new({"content-type": "application/json"}.items()) return Response.new(data, headers=headers)
use serde::{Deserialize, Serialize};use worker::*; #[derive(Deserialize, Serialize, Debug)]struct Json { hello: String,} #[event(fetch)]async fn fetch(_req: Request, _env: Env, _ctx: Context) -> Result<Response> { let data = Json { hello: String::from("world"), }; Response::from_json(&data)}