DEV Community

Cover image for How To Use HTTP Requests in Flutter
Nikhil Soman Sahu
Nikhil Soman Sahu

Posted on

How To Use HTTP Requests in Flutter

In Flutter, you can make HTTP requests using the http package. To make a request to an HTTP URL without SSL (no HTTPS), you can follow these steps:

  1. Add the http package to your pubspec.yaml file:
dependencies:
  http: ^0.13.3
Enter fullscreen mode Exit fullscreen mode

Make sure to run flutter pub get to fetch the package.

  1. Use the http package to make the HTTP request in your Dart code:
import 'package:http/http.dart' as http;

void makeHttpRequest() async {
  String url = "http://example.com";  // Replace with your HTTP URL

  try {
    http.Response response = await http.get(Uri.parse(url));

    // If the request was successful (status code 200)
    if (response.statusCode == 200) {
      print("Request successful");
      print("Response content:");
      print(response.body);
    } else {
      print("Request failed with status code ${response.statusCode}");
    }
  } catch (e) {
    print("Error making the request: $e");
  }
}

void main() {
  makeHttpRequest();
}
Enter fullscreen mode Exit fullscreen mode

Replace "http://example.com" with the actual URL you want to request.

Keep in mind that making HTTP requests without SSL may have security implications, and it is generally recommended to use HTTPS for secure communication. If possible, consider using HTTPS instead of HTTP.

Also, be aware that many modern platforms and services may require HTTPS, and some platforms might restrict or discourage the use of unsecured connections.

Always consider the security implications and requirements of your application when making these decisions. If your server supports it, consider enabling HTTPS for a more secure communication channel.

Top comments (0)