问题描述
我意识到目前至少有三个"官方"飞镖库可以使我执行HTTP请求.更重要的是,其中三个库(DART:IO(httpclient类),软件包:http和dart:html)每个都有一个不同的,不兼容的API.
截至今天,软件包:HTML不提供此功能,但是在其GitHub页面上,我发现它的目的是与DART:HTML的100%API兼容性,因此最终将在此添加这些方法.
>哪个软件包提供了最终的证明和平台独立API,以在DART中发布HTTP请求?
是包装:http?
import 'package:http/http.dart' as http; var url = "http://example.com"; http.get(url) .then((response) { print("Response status: ${response.statusCode}"); print("Response body: ${response.body}"); });
是飞镖:html/package:html?
import 'dart:html'; HttpRequest.request('/example.json') .then((response) { print("Response status: ${response.status}"); print("Response body: ${response.response}"); });
或飞镖:io?
import 'dart:io'; var client = new HttpClient(); client.getUrl(Uri.parse("http://www.example.com/")) .then((HttpClientRequest request) { // Optionally set up headers... // Optionally write to the request object... // Then call close. ... return request.close(); }) .then((HttpClientResponse response) { print("Response status: ${response.statusCode}"); print("Response body:"); response.transform(UTF8.decoder).listen((contents) { print(contents); }); });
假设我也想覆盖Android.这添加了包:混合物中的天空( https://github.com/domokit/sky_sdk/sky_sdk/a/a/a >).我承认这不是"官方" Google图书馆.
import 'package:sky/framework/net/fetch.dart'; Response response = await fetch('http://example.com'); print(response.bodyAsString());
是什么(将是)常规产品是 https://www.youtube.com/观看?v = t8xdeo8lyl8 .我想知道他们的 http请求故事将会是什么.有些东西告诉我,这将是与我们到目前为止所看到的另一只野兽.
推荐答案
html软件包是HTML解析器,允许与HTML Server端一起使用.我不会期望它会获得一些HTTPREQUEST功能.
http 包装旨在为客户端和服务器DART代码提供统一的API. dart:html中的API只是浏览器提供的API上的包装器. dart:io中的HTTPREQUEST API是在没有浏览器限制的情况下构建的,因此偏离dart:html. package:http提供了一个统一的API,该API在浏览器中运行时委派dart:html在服务器上运行时dart:io.
我认为package:http是未来的证明和跨平台,应该非常适合您的要求.
问题描述
I realized that currently there are at least three "official" Dart libraries that allow me to perform a HTTP request. What is more, three of those libraries (dart:io (class HttpClient), package:http and dart:html) have each a different, incompatible API.
As of today, package:html does not offer this functionality, but on its GitHub page I found it aims for 100% API compatibility with dart:html, so these methods will be added there eventually.
Which package provides the most future proof and platform independent API to issue a HTTP request in Dart?
Is it package:http?
import 'package:http/http.dart' as http; var url = "http://example.com"; http.get(url) .then((response) { print("Response status: ${response.statusCode}"); print("Response body: ${response.body}"); });
Is it dart:html/package:html?
import 'dart:html'; HttpRequest.request('/example.json') .then((response) { print("Response status: ${response.status}"); print("Response body: ${response.response}"); });
Or dart:io?
import 'dart:io'; var client = new HttpClient(); client.getUrl(Uri.parse("http://www.example.com/")) .then((HttpClientRequest request) { // Optionally set up headers... // Optionally write to the request object... // Then call close. ... return request.close(); }) .then((HttpClientResponse response) { print("Response status: ${response.statusCode}"); print("Response body:"); response.transform(UTF8.decoder).listen((contents) { print(contents); }); });
Let's say I want to cover Android too. That adds package:sky in the mix as well (https://github.com/domokit/sky_sdk/). I admit that this is not "official" Google library.
import 'package:sky/framework/net/fetch.dart'; Response response = await fetch('http://example.com'); print(response.bodyAsString());
What is (going to be) a regular product is https://www.youtube.com/watch?v=t8xdEO8LyL8. I wonder what their HTTP Request story is going to be. Something tells me it will be yet another different beast from all we have seen so far.
推荐答案
The html package is a HTML parser which allows to work with HTML server side. I wouldn't expect it to get some HttpRequest capabilities.
The http package aims to provide a unified API for client and server Dart code. The API in dart:html is only a wrapper over the API the browser provides. The HttpRequest API in dart:io was built without browser restrictions and thus deviates from dart:html. package:http provides an unified API which delegates to dart:html when run in the browser and to dart:io when run on the server.
I think package:http is future proof and cross-platform and should be a good fit for your requirements.