Get Access Token with REST API in flutter/dart

825 views
Skip to first unread message

Kaan Karamanoğlu

unread,
Mar 17, 2019, 7:24:59 AM3/17/19
to Flutter Dev
Hi All,

Here is my main.dart : 

Future<HttpClient> getData() async {

  Map<String, String> connection = {
    'grant_type': 'string',
    'branchcode': 'string',
    'password': 'string',
    'username': 'string',
    'dbname': 'string',
    'dbuser': 'string',
    'dbpassword': 'string',
    'dbtype': 'string+'
  };

  var uri = Uri.http("192.168.1.44:7070","api/v2/token",connection);
  http.Response r = await http.get(uri);
  print(r.statusCode);
  print(r.body);
}

ERROR ! 

I/flutter ( 9316): 400
I/flutter ( 9316): <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
I/flutter ( 9316): <HTML><HEAD><TITLE>Bad Request</TITLE>
I/flutter ( 9316): <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
I/flutter ( 9316): <BODY><h2>Bad Request - Invalid Hostname</h2>
I/flutter ( 9316): <hr><p>HTTP Error 400. The request hostname is invalid.</p>
I/flutter ( 9316): </BODY></HTML>


I cannot take access token with this getData() function, it return "Bad Request - Invalid Hostname". How can ı fix this problem ? Am ı change Future<> method, async or http methods ? 

Dshah

unread,
Mar 17, 2019, 8:15:11 AM3/17/19
to Kaan Karamanoğlu, Flutter Dev

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kaan Karamanoğlu

unread,
Mar 17, 2019, 8:20:51 AM3/17/19
to Flutter Dev
I see that topic but it don't usable for me. 

17 Mart 2019 Pazar 15:15:11 UTC+3 tarihinde Dshah H yazdı:

Giovanni Panasiti

unread,
Mar 17, 2019, 11:30:40 AM3/17/19
to Kaan Karamanoğlu, Flutter Dev

var uri = Uri.http("http://192.168.1.44:7070","api/v2/token",connection)
--
Giovanni Panasiti
 
Full Stack Developer @ Fondazione ENPAM
Full Stack Developer & Co-Founder @ MonteDelGallo.com
 
P: +39 389 97 22 602
 

Giovanni Panasiti

unread,
Mar 17, 2019, 11:43:23 AM3/17/19
to Flutter Dev
If you are working on a network with a firewall check if the computer where the emulator is running (or the phone where you are deploying your code) is allowed to access that server on that port. 

Are you able to fetch the token with a simple curl?

Also usually the http call to obtain a token is in post 

Kaan Karamanoğlu

unread,
Mar 17, 2019, 2:24:35 PM3/17/19
to Flutter Dev
Yes, when ı was ping 192.168.1.44 7070 port it is okey. ı can reach that ip adress and port but in my codes it won't running.

17 Mart 2019 Pazar 18:43:23 UTC+3 tarihinde Giovanni Panasiti yazdı:
Reply all
Reply to author
Forward
0 new messages