Get Access Token with REST API in flutter/dart


If you want to get token from rest api you have to post your connection with method.In this method you can easily declare the connection string in body field.I tried and it is working.

Here is my code block:

   Future<Null> getData() async {

        var url = "";, body:{
          "grant_type": "string",
          "branchcode": "string",
          "password": "string",
          "username": "string",
          "dbname": "string",
          "dbuser": "string",
          "dbpassword": "string",
          "dbtype": "string"
          print("Response Status: ${response.statusCode}");
          print("Response Body: ${response.body}");
Kaan Karamanoğlu
Author by

Kaan Karamanoğlu

Updated on December 10, 2022


  • Kaan Karamanoğlu
    Kaan Karamanoğlu 6 months

    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 ?

    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("","api/v2/token",connection);
      http.Response r = await http.get(uri);

    ERROR !

    I/flutter ( 9316): 400
    I/flutter ( 9316): <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""">
    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>
    • Richard Heap
      Richard Heap about 4 years
      That error is being returned by the server, so you need to investigate there to see what it doesn't like. (Previously, your C# code was sending the connection parameters as URL encoded body, but it might also work to send them as query parameters.) I'd suggest using something like Postman or another working client to prove that the request works, then compare the difference between the request you are forming in Dart and the working request. (As you are just at the stage of testing the request, consider just using a Dart program to test; you can iterate quickly using a simple cmd line prog.)
    • Kaan Karamanoğlu
      Kaan Karamanoğlu about 4 years
      I can do it in c# easily
    • Richard Heap
      Richard Heap about 4 years
      In which case you can easily compare the HTTP request sent by Dart and that sent by C#. Once you see the difference you should be able to adapt the Dart code to match. If you can't, that will be a good question.
    • Kaan Karamanoğlu
      Kaan Karamanoğlu about 4 years
      I don't now how ı can compare this two language ?
    • Richard Heap
      Richard Heap about 4 years
      I'd suggest using a packet capture tool like Wireshark to capture the request and response.
    • Kaan Karamanoğlu
      Kaan Karamanoğlu about 4 years
      I was tried with Postman. I was entered the query parameters which is my connection map. But it returns unsported_grant_type error.
  • Kaan Karamanoğlu
    Kaan Karamanoğlu about 4 years
    ı cannot do that easily. ı take all at of warnings when ı mixed with my codes.