Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't access jprq from Flutter #164

Closed
XantaKross opened this issue Aug 26, 2023 · 10 comments
Closed

Can't access jprq from Flutter #164

XantaKross opened this issue Aug 26, 2023 · 10 comments

Comments

@XantaKross
Copy link

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 can access jprq site from any browser with no issues. with the url from terminal. (JPRQ works in browser)
  • I can't access the same jprq site from flutter, not matter how I change the url from terminal (Tried http://, https://, name.jprq.live:80//, etc). (Changing url doesn't work.)
  • Finally I tried accessing other sites from the same flutter methods (from dart pub: http.get(), dio.get()) and I can access them. Like I do get a http response. (Flutter methods do access other sites with no issues.)
  • The debugger doesn't register any request when I try accessing it from flutter but it does register a request when I access it from a browser.
  • The error I get in my flutter terminal is 400 Bad request.
  • I've checked flutter docs there is nothing there that gives me a clue.
  • I tried connecting my server in a different network and my app (which is now in my phone) in a different network. Still the same error when I request it from http.get() or dio.get()

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
Copy link
Owner

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
Copy link
Author

XantaKross commented Aug 26, 2023

@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
Copy link
Owner

azimjohn commented Aug 26, 2023

I am looking 👀

@XantaKross
Copy link
Author

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

@azimjohn
Copy link
Owner

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

@azimjohn
Copy link
Owner

Found the bug, fixing it

azimjohn added a commit that referenced this issue Aug 26, 2023
@azimjohn azimjohn mentioned this issue Aug 26, 2023
@XantaKross
Copy link
Author

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

@azimjohn
Copy link
Owner

Fix is deployed, give it a try now

@azimjohn
Copy link
Owner

azimjohn commented Aug 26, 2023

Actually the issue was on Flutter/Dart's side as mentioned here:
dart-lang/sdk#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
Copy link
Author

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants