Skip to content

Return JSON

Return a Simple JSON Data.

async function onRequest({ req }) {
  const request = req.request;

  // Only accept GET Requests
  if (request.method !== "GET") return new Response("Not Allowed", {
    status: 405,
    headers: {
      Allow: "GET"
    }
  });

  const data = {
    hello: "world",
  };

  const json = JSON.stringify(data, null, 2);

  // Send the response back
  req.respondWith(new Response(json, {
    status: 200,
    headers: {
      "content-type": "application/json;charset=UTF-8",
    }
  }))
}

export { onRequest }