Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make HTTP client requests with Deno

Tags:

deno

I'm playing around with Deno but I can't find the standard HTTP client.

I also googled "deno http client" which gives me https://deno.land/x/std/http/client/, but the link is broken. Something was indexed by Google and later removed from the repo.

404 - Not Found
This file or directory could not be found.

I found 3rd party libraries soxa and deno-http but I am looking for something standard.

Is there a standard HTTP client for Deno that I am missing?

like image 683
Evandro Pomatti Avatar asked May 16 '20 13:05

Evandro Pomatti


People also ask

What are the 4 HTTP request methods?

The CONNECT method establishes a tunnel to the server identified by the target resource. The OPTIONS method describes the communication options for the target resource. The TRACE method performs a message loop-back test along the path to the target resource. The PATCH method applies partial modifications to a resource.

How to create a custom httpclient in Deno?

There is a function called createHttpClient in Deno’s core runtime. This function is used to create a custom client (with additional capabilities) and then it is passed to the fetch API. init?: RequestInit & {client: Deno.HttpClient},

How to add a custom HTTP client to Deno's fetch API?

Deno enhances the standard fetch API by adding support for a custom HTTP client. There is a function called createHttpClient in Deno’s core runtime. This function is used to create a custom client (with additional capabilities) and then it is passed to the fetch API. init?:

How to access the network layer in Deno?

An important aspect of deno: Security. When we want to access the network layer, we need to use the --allow-net flag alongside our command. Without it, you’ll get a PermissionDenied error:

How do I run my own HTTP web server?

With just a few lines of code you can run your own HTTP web server with control over the response status, request headers and more. In this example, the user-agent of the client is returned to the client: // Start listening on port 8080 of localhost. const server = Deno.listen({ port: 8080 }); console.log(`HTTP webserver running.


1 Answers

deno implements a variety of Web APIs. To make an HTTP request in deno you use fetch.

const res = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const data = await res.json();

Web APIs are exposed to the global scope the same way they're in the browser. deno aims to be browser compatible whenever it's possible.

You'll also be able to use:

  • FormData
  • Headers
like image 100
Marcos Casagrande Avatar answered Sep 17 '22 20:09

Marcos Casagrande