0

I'm trying to learn Flutter/Dart and I'm having to much problems. Now I'm trying to obtain some values from an API. My code is:

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:myapp01_apirequest/src/models/uplink_models.dart';

class UplinksProvider{
  String _url       = 'xxx.yyy.com';

  Future<List<Uplink>> getEnCines() async{
    try{
      final url = Uri.https(_url, ':14442/api/external/login', {
        'username': 'Joe689',
        'password': '15.Job_1825zz'
      });

      final resp = await http.get(url);
      final decodedData = json.decode(resp.body);
      print('Patata');
      print(decodedData);
      
      return [];
    }catch (error){
      print('++++++++++///////++++++++++++++++');
      print(error);
      print('++++++++++*******++++++++++++++++');
    }
  }
}

Reading the Uri constructor documentation I understood that I have to split in 3 my url.

  • First one is authority. In my case I think is xxx.yyy.com.
  • Second is the unencodedPath. In my case I think is :14442/api/external/login.
  • Finally a map with params in my case the username and pass (the only thing I'm pretty sure is correct in my code).

If I do this, any problem appears, but the print('Patata'); and print(decodeData); don't appear. In addition, a file called io_client.dart opens and marks the next line.

var ioRequest = (await _inner.openUrl(request.method, request.url))

The console shows nothing (I think):

Launching lib\main.dart on LG M700 in debug mode...
lib\main.dart:1
Formato de par�metros incorrecto:
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:55883/KBAtv2-Jljc=/ws

Why no errors appears but I can't obtain my desired data?


EDIT:

According for what @Preet Shah's said I press the "VS run button" and appears the following exception three times:

I/flutter ( 1932): ++++++++++///////++++++++++++++++
I/flutter ( 1932): HandshakeException: Handshake error in client (OS Error:
I/flutter ( 1932):  CERTIFICATE_VERIFY_FAILED: Hostname mismatch(handshake.cc:354))
I/flutter ( 1932): ++++++++++*******++++++++++++++++

And finally appears:

I/Choreographer( 1932): Skipped 148531 frames!  The application may be doing too much work on its main thread.
D/vndksupport( 1932): Loading /vendor/lib/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
D/vndksupport( 1932): Loading /vendor/lib/hw/gralloc.msm8937.so from current namespace instead of sphal namespace.
I/Choreographer( 1932): Skipped 35 frames!  The application may be doing too much work on its main thread.

Then the problem is a certification problem, CERTIFICATE_VERIFY_FAILED. As I feel more comfortable with python, I have done some tests to understand the problem. I have been able to verify that this API requires having all the certification verifications in false, otherwise it never leaves the loop. Here is my Python code (just to show what I'm saying).

import requests
import json

log_params = {'username': 'Joe689', 'password': '15.Job_1825zz'}
headers = {'Content-type': 'application/json'}
url = 'https://xxx.yyy.com:14442/api/external/login'
 
response = requests.post(url, data=json.dumps(params), headers=self.headers, verify=False)
finalRes = json.loads(response.text)

As I said, this code is just for me to understand the problem because I am a newbie to Dart. Here I found this answer and it seems has the solution but I don't know how to implement it, using my Uri.https estructure (maybe it's not possible).

I tried this, but isn't working:

Map<String, String> requestHeaders = {
    'Content-type': 'application/json'
};
final resp = await http.get(url, headers:requestHeaders);

Thank you very much.

Lleims
  • 1,275
  • 12
  • 39
  • 1
    The line that is marked in the file `io_client.dart` has some exception or error related to it. VS Code usually shows the exception before printing it in the log. What you can do is press the "play" button in VS Code's "flutter run" console. Or just see if any exception is shown and share it here. – Preet Shah Feb 25 '21 at 14:18
  • I did not know that! I have modified the question thanks to the new information. – Lleims Feb 25 '21 at 15:58
  • Well, there's not much I can do to help. However, I understand that the answer you have found, you are not able to implement it. So, I will try to figure out how to implement it. – Preet Shah Feb 26 '21 at 06:46

1 Answers1

0

Try this:

HttpClient client = new HttpClient();
client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);

String url ='xxx.yyy.com:14442/api/external/login';

Map map = { 
     "email" : "Joe689" , 
     "password" : "15.Job_1825zz"
};

HttpClientRequest request = await client.getUrl(Uri.parse(url));

request.headers.set('content-type', 'application/json');

request.add(utf8.encode(json.encode(map)));

HttpClientResponse response = await request.close();

String reply = await response.transform(utf8.decoder).join();

print(reply);

Now, check what reply contains. And accordingly, return the data.

Preet Shah
  • 792
  • 5
  • 12