Does the Dart console application have a library for an HTTP request that does not require DOM access?

I started by using HTTPRequest in dart:html , but quickly realized that this was not possible in a console application. I fulfilled some Google searches, but cannot find what I need (just find an HTTP server), is there a way to send a regular HTTP request through a console application?

Or will I need to use the socket method and implement my own HTTP request?

+4
source share
2 answers

In the IO library, there is an HttpClient class for creating HTTP requests:

 import 'dart:io'; void main() { HttpClient client = new HttpClient(); client.getUrl(Uri.parse("http://www.dartlang.org/")) .then((HttpClientRequest request) { return request.close(); }) .then(HttpBodyHandler.processResponse) .then((HttpClientResponseBody body) { print(body.body); }); } 

Update: Since the HttpClient is rather low level and a bit awkward for something simple like this, the main Dart team also made pub , http , which makes it easier:

 import 'package:http/http.dart' as http; void main() { http.get('http://pub.dartlang.org/').then((response) { print(response.body); }); } 

I found that the crypto package was dependent, so my pubspec.yaml looks like this:

 name: app-name dependencies: http: any crypto: any 
+8
source

You will look for HttpClient , which is part of the dart:io server-side SDK library.

Example taken from the API document linked above:

 HttpClient client = new HttpClient(); client.getUrl(Uri.parse("http://www.example.com/")) .then((HttpClientRequest request) { // Prepare the request then call close on it to send it. return request.close(); }) .then((HttpClientResponse response) { // Process the response. }); 
+2
source

Source: https://habr.com/ru/post/1487109/


All Articles