request http

This commit is contained in:
Valentin CZERYBA 2022-09-01 00:08:13 +02:00
parent 189c866f58
commit b2f15acda8

View File

@ -1,3 +1,5 @@
// ignore_for_file: unnecessary_brace_in_string_interps
import 'dart:io';
import 'package:flutter/material.dart';
@ -55,18 +57,46 @@ class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Future<void> _incrementCounter() async {
var url = Uri.parse("http://localhost:8083/api/v1/token");
var url = Uri.parse("http://localhost:8083/api/token");
String credentials = "peter93:toto";
Codec<String, String> stringToBase64 = utf8.fuse(base64);
String encoded = stringToBase64.encode(credentials);
stderr.writeln('Basic $encoded');
var response = await http.get(url, headers: {
HttpHeaders.authorizationHeader: 'Basic $encoded',
});
stderr.writeln('Response status: ${response.headers}');
var user = "";
var jwt = "";
var cookies = response.headers["set-cookie"].toString().split(";");
for (var cookie in cookies) {
var cookiesMany = cookie.split(",");
for (var cookie2 in cookiesMany) {
switch (cookie2.split("=")[0]) {
case "jwt":
{
jwt = cookie2.toString();
}
break;
case "user":
{
user = cookie2.toString();
}
break;
default:
break;
}
}
}
stderr.writeln("User ${user}");
stderr.writeln("Jwt ${jwt}");
stderr.writeln('Response status: ${response.statusCode}');
stderr.writeln('Response body: ${response.body}');
var urlGet = Uri.parse("http://localhost:8083/api/users");
var responseGet = await http
.get(urlGet, headers: {HttpHeaders.cookieHeader: '${jwt}; ${user}'});
stderr.writeln('Response Get status: ${responseGet.statusCode}');
stderr.writeln('Response Get body: ${responseGet.body}');
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below