From b2f15acda89bd52abbb0c3694b446d38e42040aa Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 1 Sep 2022 00:08:13 +0200 Subject: [PATCH] request http --- covas_mobile/lib/main.dart | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/covas_mobile/lib/main.dart b/covas_mobile/lib/main.dart index ed1de44..12dc564 100644 --- a/covas_mobile/lib/main.dart +++ b/covas_mobile/lib/main.dart @@ -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 { int _counter = 0; Future _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 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