Return string from HttpRequest - dart

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;
}

Related

Async interceptors (gRPC dart)

I'm using gRPC generated clients for dart and I need to add Authorization header to every request. I know there's ClientInterceptor but I want to ask if there is any way to call async method in interceptor's method because I have this async method for retrieving token:
static Future<String> getToken() async {
final CognitoAuthSession session = await Amplify.Auth.fetchAuthSession(
options: CognitoSessionOptions(getAWSCredentials: true)
);
return session.userPoolTokens.idToken;
}
After some more googling I found out that MetadataProvider return FutureOr<void>, so you can call async methods.
Final interceptor's code
class AuthenticationInterceptor extends ClientInterceptor {
FutureOr<void> _provider(Map<String, String> metadata, String uri) async {
final token = await AuthenticationService.getToken();
metadata['Authorization'] = "Bearer $token";
}
#override
ResponseFuture<R> interceptUnary<Q, R>(ClientMethod<Q, R> method, Q request, CallOptions options, invoker) {
return super.interceptUnary(
method,
request,
options.mergedWith(CallOptions(providers: [_provider])),
invoker
);
}
}
And you can use it like
final client = YourGrpcClient(clientChannel, interceptors: [AuthenticationInterceptor()]);

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 do I send multiple API request once?

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();
}

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);
}

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