Get Access Token with REST API in flutter/dart
11,114
If you want to get token from rest api you have to post your connection with http.post() 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 = "http://192.168.1.23:7070/api/v2/token";
http.post(url, body:{
"grant_type": "string",
"branchcode": "string",
"password": "string",
"username": "string",
"dbname": "string",
"dbuser": "string",
"dbpassword": "string",
"dbtype": "string"
}).then((response){
print("Response Status: ${response.statusCode}");
print("Response Body: ${response.body}");
});
}
Author by
Kaan Karamanoğlu
Updated on December 10, 2022Comments
-
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("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>
-
Richard Heap about 4 yearsThat 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 about 4 yearsI can do it in c# easily
-
Richard Heap about 4 yearsIn 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 about 4 yearsI don't now how ı can compare this two language ?
-
Richard Heap about 4 yearsI'd suggest using a packet capture tool like Wireshark to capture the request and response.
-
Kaan Karamanoğlu about 4 yearsI 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 about 4 yearsı cannot do that easily. ı take all at of warnings when ı mixed with my codes.