2

How can I make post requests from flutter.I need to authenticate a user with his email address and password. Please help

tried with the following code

 http.post(url, body: {"email": "email", "password": "password"})
      .then((response) {
    print("Response status: ${response.statusCode}");
    //print("Response body: ${response.body}");
  });

getting "firewall read failed or timeout" error

Shyju
  • 63
  • 1
  • 6
  • 1
    firewall read failed or timeout - this sounds like a network problem. – Burhan Khalid Mar 21 '18 at 04:52
  • but I am running on my physical device – Shyju Mar 21 '18 at 05:05
  • Could you post the log of the error including stacktrace? That might help. Also, could you try doing a http.get from whichever server you're trying to access, as that can tell us you're having a problem accessing the server or with the actual request you're trying to do. – rmtmckenzie Mar 21 '18 at 23:37

1 Answers1

3

I use

import 'dart:async' show Future;
import 'dart:io'
    show HttpClient, HttpClientBasicCredentials, HttpClientCredentials;

import 'package:http/http.dart' show BaseClient, IOClient;

typedef Future<bool> HttpAuthenticationCallback(
    Uri uri, String scheme, String realm);

HttpAuthenticationCallback _basicAuthenticationCallback(
        HttpClient client, HttpClientCredentials credentials) =>
    (Uri uri, String scheme, String realm) {
      client.addCredentials(uri, realm, credentials);
      return new Future.value(true);
    };

BaseClient createBasicAuthenticationIoHttpClient(
    String userName, String password) {
  final credentials = new HttpClientBasicCredentials(userName, password);

  final client = new HttpClient();
  client.authenticate = _basicAuthenticationCallback(client, credentials);
  return new IOClient(client);
}
final http =
    createBasicAuthenticationIoHttpClient(_config.userName, _config.password);

http.get(...)
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    This was very helpful. Thanks for sharing! – Wahib Ul Haq Apr 25 '18 at 22:40
  • And how handle 401 status? – Augusto Feb 06 '19 at 15:45
  • @Augusto what are you trying to accomplish? – Günter Zöchbauer Feb 06 '19 at 15:51
  • @GünterZöchbauer I have a rest API (in ruby on rails) with return 401 when it's unauthorized (user not have access to the specified area, for example). If I use 400 error in this backend, flutter handle perfectly, but not in 401 – Augusto Feb 07 '19 at 18:15
  • Interesting. I don't remember in what context I used this exactly. I think it was to access some Graph database server (Virtuoso or Stardog). I don't know what they respond with. Did you check https://flutter.io/docs/cookbook/networking/authenticated-requests ? https://stackoverflow.com/questions/18519413/dart-client-httprequest-error-authentication-handling, https://stackoverflow.com/questions/50305479/dart-httpclient-with-basic-authentication-issues-after-flutter-upgrade might also help. – Günter Zöchbauer Feb 07 '19 at 18:30