I'm trying to make a get request but I need to put the cookie.
Using the curl works:
curl -v --cookie "sessionid=asdasdasqqwd" <my_site>
But the function below does not bring anything
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' as parser;
import 'package:html/dom.dart';
...
parseHtml() async {
http.Response response = await http.get (
<my_site>,
headers: {"sessionid": "asdasdasqqwd"}
);
Document document = parser.parse (response.body);
print(document.text);
}
Would there be any way to put the cookie on the get request in Dart?
You could use the http.get(Url, Headers Map)
function and manually create your cookies in the header map, but it is easier to make a request with cookies included by using HttpClient
:
import 'dart:convert';
import 'dart:io';
import 'package:html/dom.dart';
import 'package:html/parser.dart' as parser;
parseHtml() async {
HttpClient client = new HttpClient();
HttpClientRequest clientRequest =
await client.getUrl(Uri.parse("http: //www.example.com/"));
clientRequest.cookies.add(Cookie("sessionid", "asdasdasqqwd"));
HttpClientResponse clientResponse = await clientRequest.close();
clientResponse.transform(utf8.decoder).listen((body) {
Document document = parser.parse(body);
print(document.text);
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With