azimjohn / jprq

join public router. quickly.
https://jprq.io
973 stars 145 forks source link

Can't access jprq from Flutter #164

Closed XantaKross closed 1 year ago

XantaKross commented 1 year ago

I've been developing a flutter app in Android studio for sometime now. And I created the backend using pycharm and django.

What I've done until now:

I do not understand what all of these means it is quite frankley very very confusing. Can someone help asap, I need to complete this project soon.

azimjohn commented 1 year ago

Hello @XantaKross

Interesting issue. Could you take a look at the HTTP version flutter "http" lib is using? For a quick fix, can try using HTTP 1.1?

XantaKross commented 1 year ago

@azimjohn Thank you for replying so soon. It still gives me bad request. I will add my code so that it might be helpful for you to debug.

My pubspec.yaml file (used for dependency config):

    dependencies:
      flutter:
        sdk: flutter
      cupertino_icons: ^1.0.2
      path_provider:
      cunning_document_scanner: ^1.1.2
      cookie_jar: ^4.0.8
      dio: ^5.0.0
      dio_cookie_manager: 
      http: ^1.1.0
      csv: ^5.0.0 

The dart code through which I'm trying to access the hosted server:

String Host = "https://adapt.jprq.live"; // app link declaration

class AuthSession {
  var endpoint = "$Host/SignLogIn"; // setting my specific endpoint.
  String? csrfToken;
  final dio = Dio(); 

  // I was actually trying to do this with dio (another library for http and https requests but powerful and complex than http), but 
  // when it failed I tried it with http in order to check if it was a problem with dio itself. Or not.

  final cookieJar = CookieJar();

  Map<String, dynamic> headers = {"set-cookie":null}; 

  @override
  void initState() { // this part is ignorable as it was for the dio implementation.
    dio.options.connectTimeout = 5000 as Duration?; // 5 seconds
    dio.options.receiveTimeout = 5000 as Duration?; // 5 seconds
    dio.options.followRedirects = true;
    dio.interceptors.add(CookieManager(cookieJar));
    dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: true));

  }

  Future<void> getCookie() async {
    endpoint = "https://adapt.jprq.live/SignLogIn"; // manually setting up the endpoint.
    print(1);
    print(endpoint);
    print(2);
    print(headers);
    var response = (await http.get(Uri.parse(endpoint)));
    print(3);
    print(response.headers);
    print(response.body);

    // This part can also be ignored as it was for dio.
    // String setCookieHeader = '';
    // for (String header in response.headers['set-cookie']!) {
    //   if (header.startsWith('csrftoken=')) {
    //     setCookieHeader = header;
    //     break;
    //   }
    // }
    // csrfToken = setCookieHeader.split(';')[0].split('=')[1];
    // headers["Cookie"] = 'csrftoken=$csrfToken';
    // headers["X-CSRFToken"] = csrfToken;

  }

The Output: Launching lib\main.dart on Windows in debug mode... Building Windows application... Nuget.exe not found, trying to download or use cached version. √ Built build\windows\runner\Debug\gold_bucket.exe. Debug service listening on ws://127.0.0.1:63609/a3zQLoH8z-k=/ws Syncing files to device Windows... flutter: 1 flutter: https://adapt.jprq.live/SignLogIn flutter: 2 flutter: {set-cookie: null} flutter: 3 flutter: {content-length: 11} flutter: Bad Request

azimjohn commented 1 year ago

I am looking 👀

XantaKross commented 1 year ago

I'm sorta new to this whole thing (;テ﹏テ)

azimjohn commented 1 year ago

I could replicate the issue on my end. Looking closer 👀

azimjohn commented 1 year ago

Found the bug, fixing it

XantaKross commented 1 year ago

bro T_T you're a life saver. thank you.

azimjohn commented 1 year ago

Fix is deployed, give it a try now

azimjohn commented 1 year ago

Actually the issue was on Flutter/Dart's side as mentioned here: https://github.com/dart-lang/sdk/issues/33501

Anyways jprq now works with Flutter too, thanks for reporting this issue.

If you want, consider buying me a coffee: https://buymeacoffee.com/azimjon

XantaKross commented 1 year ago

Oh cool.

will check out the fix rn.

Rn, I am a freelance student just trying to build my first app lol. But I will definitely buy you an coffee when I succesfully make my first sell :)