Deno LandDeno

Deploy Docs

Make an outbound request

Deno Deploy Runtime supports the Fetch API that's available in browsers to make HTTP requests to external services. In this post let's take a look at how we can make GET and POST requests using fetch().

Make a GET request:

async function handleRequest(request) {
  // We pass the url as the first argument to fetch and an object with
  // additional info like headers, method, and body for POST requests as
  // the second argument. By default fetch  makes a GET request,
  // so we can skip specifying method for GET requests.
  const response = await fetch("https://api.github.com/users/denoland", {
    headers: {
      // Servers use this header to decide on response body format.
      // "application/json" implies that we accept the data in JSON format.
      accept: "application/json",
    },
  });

  // The .ok property of response indicates that the request is
  // sucessfull (status is in range of 200-299).
  if (response.ok) {
    // response.json() method reads the body and parses it as JSON.
    // It then returns the data in JavaScript object.
    const { name, login, avatar_url } = await response.json();
    return new Response(
      JSON.stringify({ name, username: login, avatar: avatar_url }),
      {
        headers: {
          "content-type": "application/json; charset=UTF-8",
        },
      },
    );
  }
  // fetch() doesn't throw for bad status codes. You need to handle them
  // by checking if the response.ok is true or false.
  // In this example we're just returning a generic error for simplicity but
  // you might want to handle different cases based on response status code.
  return new Response(
    JSON.stringify({ message: "couldn't process your request" }),
    {
      status: 500,
      headers: {
        "content-type": "application/json; charset=UTF-8",
      },
    },
  );
}

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});

Make a POST request:

async function handleRequest(request) {
  // For making a POST request we need to specify the method property
  // as POST and provide data to the body property in the same object.
  // https://post.deno.dev echoes data we POST to it.
  const response = await fetch("https://post.deno.dev", {
    method: "POST",
    headers: {
      // This headers implies to the server that the content of
      // body is JSON and is encoded using UTF-8.
      "content-type": "application/json; charset=UTF-8",
    },
    body: JSON.stringify({
      message: "Hello from Deno Deploy.",
    }),
  });

  if (response.ok) {
    // The echo server returns the data back in
    const {
      json: { message },
    } = await response.json();
    return new Response(JSON.stringify({ message }), {
      headers: {
        "content-type": "application/json; charset=UTF-8",
      },
    });
  }

  return new Response(
    JSON.stringify({ message: "couldn't process your request" }),
    {
      status: 500,
      headers: {
        "content-type": "application/json; charset=UTF-8",
      },
    },
  );
}

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});

In the above example we've see how to post JSON data. You can learn more about the Fetch API to better use it.


Please file an issue in our feedback repository if you find any problem in Deploy service or documentation.