How to solve Flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request?

Cover Image for How to solve Flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

👋 Hey there, Flutter enthusiast! 😄 Are you experiencing the dreaded CERTIFICATE_VERIFY_FAILED error while trying to perform a POST request in your Flutter app? Don't fret! 🙌 This blog post is here to help you solve this issue in a jiffy. Let's dive right in, shall we? 💪

Understanding the Problem

The CERTIFICATE_VERIFY_FAILED error occurs when the SSL/TLS certificate presented by the server during the handshake process cannot be verified by the client. This often happens when the client is unable to find the necessary root certificates to establish a secure connection.

Common Causes

There are a few common causes for this error:

  1. Missing Root Certificates: The client may not have the required root certificates installed to verify the server's certificate.

  2. Invalid or Self-Signed Certificates: The server may be using an invalid or self-signed certificate that is not trusted by the client by default.

  3. Hostname Mismatch: The hostname in the certificate does not match the actual hostname of the server.

Easy Solutions

Now that we know what might be causing the error, here are three easy solutions to fix it:

Solution 1: Bypass SSL Verification (Not recommended for production)

If you are using Flutter for development purposes, you can opt to bypass SSL verification to establish a connection without validating the server's certificate. However, keep in mind that this approach is not suitable for production environments, as it undermines the security of the connection.

To bypass SSL verification, you can make use of the http package's IOClient and HttpClient classes as follows:

import 'dart:io';
import 'package:http/http.dart' as http;

void main() {
  // Bypass SSL verification
  HttpClient httpClient = HttpClient()
    ..badCertificateCallback = ((X509Certificate cert, String host, int port) => true);

  final ioClient = http.IOClient(httpClient);

  // Use ioClient for your requests
Solution 2: Install Root Certificates

If the error is due to missing root certificates, you can manually add them to your Flutter app. Here's how:

  1. Visit the website of the server you are trying to connect to in your browser.

  2. View the certificate details (usually found in the browser's security options).

  3. Download the root certificate (usually in PEM or CRT format).

  4. Create a certs folder in your Flutter project's root directory.

  5. Move the downloaded root certificate file into the certs folder.

  6. Update your pubspec.yaml file to include the certificate in your app's assets:

    - certs/
  1. In your Dart code, use the rootBundle to load the certificate and provide it to the http package:

import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:flutter/services.dart' show rootBundle;

void main() async {
  // Load the certificate
  final certFileData = await rootBundle.load('certs/my_root_certificate.crt');
  SecurityContext securityContext = SecurityContext.defaultContext;

  final httpClient = http.Client()
    ..forceUgrade = true
    ..badCertificateCallback = ((X509Certificate cert, String host, int port) => true)
    ..context = securityContext;

  // Use httpClient for your requests
Solution 3: Use a SafeHTTP Package

Alternatively, you can use a safe HTTP package that handles certificate validation for you and simplifies the process. One such package is the flutter_secure_http package.

To use flutter_secure_http, add it to your pubspec.yaml file:

  flutter_secure_http: ^3.0.0

Here's an example of how to use it:

import 'package:flutter_secure_http/flutter_secure_http.dart';

void main() {
  final http = FlutterSecureHttp();

  // Use http for your requests

Wrapping It Up

And there you have it! Three easy solutions to help you resolve the CERTIFICATE_VERIFY_FAILED error in Flutter. You can choose the approach that best suits your requirements and get back to coding awesomeness in no time! 💥

If you found this blog post helpful, don't forget to share it with other Flutter developers facing the same challenge. 😊 And if you have any questions or other tricks up your sleeve, share them in the comments below. Let's solve problems and build amazing Flutter apps together! 🚀✨

More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello