Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between http and HttpClient in Dart

I'm trying to understand the difference between the http package and the HttpClient class in the dart:io library. My purpose is to known when I should use which. I see both of them used to apparently do the same thing.

I've seen these Q&As:

  • Choosing between package:html, dart:html, dart:io (class HttpClient) and package:http APIs to fetch HTTP resources
  • How to make HTTPS request using HttpClient in dart?
  • In a Dart console application, is there a library for an HTTP Request that doesnt require DOM access?
  • How to do POST in Dart command line HttpClient

This is what I think is true so far but my understanding is fuzzy:

  • http is high level and HttpClient is low level (source)
  • http can make post requests but HttpClient can't (source)
  • both http and HttpClient (with HttpClientRequest) can make GET and POST requests (source)
  • both http and HttpClient can be used on the client and the server

So to sum it up, I would say that each one can do anything that the other can as well, but it is easier to use the http package since this one is more high-level. Is that summary correct?

like image 623
Suragch Avatar asked Feb 27 '19 18:02

Suragch


People also ask

What is difference between HTTP and HttpClient?

The HttpClient is used to perform HTTP requests and it imported form @angular/common/http. The HttpClient is more modern and easy to use the alternative of HTTP. HttpClient is an improved replacement for Http. They expect to deprecate Http in Angular 5 and remove it in a later version.

What is HttpClient in flutter?

An HTTP client for communicating with an HTTP server. Sends HTTP requests to an HTTP server and receives responses. Maintains state, including session cookies and other cookies, between multiple requests to the same server. Note: HttpClient provides low-level HTTP functionality.

What is HTTP Dart?

A composable, Future-based library for making HTTP requests. This package contains a set of high-level functions and classes that make it easy to consume HTTP resources. It's multi-platform, and supports mobile, desktop, and the browser.

What is HTTP package flutter?

Flutter provides an http package that supports making HTTP requests. In this article, you will create an example Flutter app that uses the http package to perform HTTP requests to display placeholder information.


1 Answers

The http package is an abstraction over dart:io and dart:html.

So if you want to share code between browser and other platforms that makes HTTP requests, then it's best to use the http package. Then the code will just work everywhere.

If you don't care about the browser use what API you like best. In Flutter the http package just wraps dart:io's HttpClient.

like image 135
Günter Zöchbauer Avatar answered Sep 28 '22 09:09

Günter Zöchbauer