How do I send multiple API request once? - dart

currently, I used http, I found a way to send multiple requests once to react using axios.
axios.all([
axios.get('http://google.com'),
axios.get('http://apple.com')
])
.then(axios.spread((googleRes, appleRes) => {
// do something with both responses
});
Like this Is that any way to send multiple requests once?

#mezoni answer is correct. But this is less code with caching also.
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() async {
final urlList = ['http://google.com', 'http://apple.com'];
final responses = await Future.wait(
urlList.map((String url) {
return http.get(url);
}),
);
final List<dynamic> caches = responses.map((response) {
return json.decode(response.body);
}).toList();
}

Related

Reading data from url in List

I know I can read data from URL as:
import 'dart:convert';
import 'dart:io';
new HttpClient().getUrl(Uri.parse('https://docs.google.com/spreadsheets/d/e/2PACX-1vQvf9tp4-fETDJbC-HRmRKvVFAXEAGO4lrYPpVeiYkB6nqqXdSs3CjX0eBMvjIoEeX9_qU6K2RWmzVk/pub?gid=0&single=true&output=csv'))
.then((HttpClientRequest request) => request.close())
.then((HttpClientResponse response) => response.transform(new Utf8Decoder()).listen(print));
Where the above print the response I'm getting.
Also, I know that the read string can be put into a file by replacing the last statement to be:
.then((HttpClientResponse response) => response.pipe(new File('foo.txt').openWrite()));
In the other hand, I know I can put a string into CSV as:
// dependencies: csv: ^4.0.3
import 'package:csv/csv.dart';
List<List<dynamic>> rowsAsListOfValues = const CsvToListConverter().convert(yourString);
But how can I combine them together, so that I read the data from url using http as shown above, and decode the returned response into csv variable?
I think you will have an easier time if you mark your method with async so you can use the await keyword instead of trying using the .then() method. So something like this:
import 'dart:convert';
import 'dart:io';
import 'package:csv/csv.dart';
Future<void> main() async {
print(await csv());
}
Future<List<List<dynamic>>> csv() async {
final request = await HttpClient().getUrl(Uri.parse(
'https://docs.google.com/spreadsheets/d/e/2PACX-1vQvf9tp4-fETDJbC-HRmRKvVFAXEAGO4lrYPpVeiYkB6nqqXdSs3CjX0eBMvjIoEeX9_qU6K2RWmzVk/pub?gid=0&single=true&output=csv'));
final response = await request.close();
final csvString = await response.transform(const Utf8Decoder()).first;
return const CsvToListConverter().convert(csvString);
}
You can read more about this here: https://dart.dev/codelabs/async-await

How can I write uploaded multipart files to disk?

I'm trying to handle file upload through multipart requests with Aqueduct. Aqueduct has now an example on how to handle multipart requests here:
https://aqueduct.io/docs/http/request_and_response/#example-multipartform-data
The example explains, how to get the header and the content of the files. However it doesn't explain how to write the content into a file on disk.
How can I write the content of the files uploaded to disk?
Below an example that shows what I want to achieve, but doesn't work:
import 'dart:io';
import 'package:aqueduct/aqueduct.dart';
import 'package:mime/mime.dart';
class MediaUploadController extends ResourceController {
MediaUploadController() {
acceptedContentTypes = [ContentType("multipart", "form-data")];
}
#Operation.post()
Future<Response> postMultipartForm() async {
final transformer = MimeMultipartTransformer(request.raw.headers.contentType.parameters["boundary"]);
final bodyStream = Stream.fromIterable([await request.body.decode<List<int>>()]);
final parts = await transformer.bind(bodyStream).toList();
for (var part in parts) {
final String contentType = part.headers["content-type"];
// Write content to disk
final content = await part.toList();
final fileName = DateTime.now().millisecondsSinceEpoch.toString() + ".jpg";
var file = new File('data/' + fileName);
var sink = file.openWrite();
sink.write(content);
sink.close();
}
return new Response.ok({});
}
}
This below actually worked. Additionally to the mime package, I have also added the http_server package to pubspec.yaml, because it makes it easier to handle the multipart form data.
dependencies:
aqueduct: ^3.0.1
mime: ^0.9.6+2
http_server: ^0.9.8+1
Then I studied some other frameworks to see how they handled writing to the file. It's so complicated to get how this multipart stuff and streams work together. But at last after nearly a week, the light at the end of the tunnel... until the next questions pop up. Most likely 10 minutes down the line :)
import 'dart:io';
import 'package:aqueduct/aqueduct.dart';
import 'package:mime/mime.dart';
import 'package:http_server/http_server.dart';
class MediaUploadController extends ResourceController {
MediaUploadController() {
acceptedContentTypes = [ContentType("multipart", "form-data")];
}
#Operation.post()
Future<Response> postMultipartForm() async {
final transformer = MimeMultipartTransformer(request.raw.headers.contentType.parameters["boundary"]);
final bodyStream = Stream.fromIterable([await request.body.decode<List<int>>()]);
final parts = await transformer.bind(bodyStream).toList();
for (var part in parts) {
HttpMultipartFormData multipart = HttpMultipartFormData.parse(part);
final ContentType contentType = multipart.contentType;
final content = multipart.cast<List<int>>();
final filePath = "data/" + DateTime.now().millisecondsSinceEpoch.toString() + ".jpg";
IOSink sink = File(filePath).openWrite();
await for (List<int> item in content) {
sink.add(item);
}
await sink.flush();
await sink.close();
}
return new Response.ok({});
}
}

Make a http request in dart whith dart:io

Hey I'm a beginner and I want to interact with an API with dart:io for fetch JSON files I can fetch the data with this code :
final HttpClient client = HttpClient();
client.getUrl(Uri.parse("https://api.themoviedb.org/3/movie/76341?api_key=fbe54362add6e62e0e959f0e7662d64e&language=fr"))
.then((HttpClientRequest request) {
return request.close();
})
.then((HttpClientResponse response) {
Map a;
print(a);
But I want to have a Map whith the JSON but I can't do it. If I could get a String that contains the JSON I could do it with
json.decode();
also know that the answer is stored in an int list that represents the utf8 values of the characters so with utf8.decode(responce.toList()) I can get the utf8 value but responce.toList() return a Future but even if it may be easy I don't know how to get the list.
import 'dart:convert';
import 'dart:io';
void main() async {
final client = HttpClient();
final request = await client.getUrl(Uri.parse(
'https://api.themoviedb.org/3/movie/76341?api_key=fbe54362add6e62e0e959f0e7662d64e&language=fr'));
final response = await request.close();
final contentAsString = await utf8.decodeStream(response);
final map = json.decode(contentAsString);
print(map);
}

Return string from HttpRequest

In Dart I can do:
await HttpRequest.getString(path)
and this will return a string.
I want to create a method that will do the same, but like this:
HttpRequest request = new HttpRequest();
request
..open('Get',getPath)
..setRequestHeader('Content-Type','application/json')
..send('');
...
return responseString;
I can do it using events and futures, but I would like to understand how to do it with async & await specifically.
Edit:
This is for the dart:html HttpRequest for browser.
Haven't tried but I guess this is what you're looking for
import 'dart:html';
import 'dart:async';
main() async {
print(await getString());
}
Future<String> getString() async {
String getPath = 'https://dartpad.dartlang.org/';
HttpRequest request = new HttpRequest();
request
..open('Get',getPath)
..setRequestHeader('Content-Type','application/json')
..send('');
// request.onReadyStateChange.listen(print);
await request.onLoadEnd.first;
return request.responseText;
}

How do I fetch URL from a Dart server (dart:io)?

While fetching a URL in on the client (dart:html) is straightforward, the server side (dart:io) doesn't have the handy getString method.
How do I simply load a URL document as a String?
Use the http package and read function that returns the response body as a String:
import 'package:http/http.dart' as http;
void main() {
http.read("http://httpbin.org/").then(print);
}
This should work on the server
import 'package:http/http.dart' as http;
void main(List<String> args) {
http.get("http://www.google.com").then((http.Response e) => print(e.statusCode));
}
This will help:
import "dart:io";
import "dart:async";
import "dart:convert";
Future<String> fetch(String url) {
var completer = new Completer();
var client = new HttpClient();
client.getUrl(Uri.parse(url))
.then((request) {
// Just call close on the request to send it.
return request.close();
})
.then((response) {
// Process the response through the UTF-8 decoder.
response.transform(const Utf8Decoder()).join().then(completer.complete);
});
return completer.future;
}
You would use this method/function like this:
fetch("http://www.google.com/").then(print);
This gets the job done, but please note that this is not a robust solution. On the other hand, if you're doing anything more than a command-line script, you'll probably need more than this anyway.

Resources