Skip to main content
  1. Posts/

<time datetime="0001-01-01 00:00:00 &#43;0000 UTC">1 January 0001</time><span class="px-2 text-primary-500">&middot;</span><span title="Reading time">2 mins</span><span class="px-2 text-primary-500">&middot;</span> <span class="mb-[2px]"> <a href="https://github.com/cgutierr-zgz/cgutierr-zgz.github.io/edit/main/content/posts/sopa.md" class="text-lg hover:text-primary-500" rel="noopener noreferrer" target="_blank" title=""><span class="relative inline-block align-text-bottom px-1 icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M490.3 40.4C512.2 62.27 512.2 97.73 490.3 119.6L460.3 149.7L362.3 51.72L392.4 21.66C414.3-.2135 449.7-.2135 471.6 21.66L490.3 40.4zM172.4 241.7L339.7 74.34L437.7 172.3L270.3 339.6C264.2 345.8 256.7 350.4 248.4 353.2L159.6 382.8C150.1 385.6 141.5 383.4 135 376.1C128.6 370.5 126.4 361 129.2 352.4L158.8 263.6C161.6 255.3 166.2 247.8 172.4 241.7V241.7zM192 63.1C209.7 63.1 224 78.33 224 95.1C224 113.7 209.7 127.1 192 127.1H96C78.33 127.1 64 142.3 64 159.1V416C64 433.7 78.33 448 96 448H352C369.7 448 384 433.7 384 416V319.1C384 302.3 398.3 287.1 416 287.1C433.7 287.1 448 302.3 448 319.1V416C448 469 405 512 352 512H96C42.98 512 0 469 0 416V159.1C0 106.1 42.98 63.1 96 63.1H192z"/></svg> </span> Suggest an edit</a> </span>

title: “Retry, log and refresh auth tokens with HTTP” date: 2022-11-16 tags: [“flutter”, “dart”, “http”, “interceptors”, “retry”, “logger”] summary: “Learn how to use http package in Dart to refresh tokens and retry failed requests in a Flutter app.” draft: false showTableOfContents: true showEdit: true showAuthor: true sourceCode: “https://github.com/cgutierr-zgz/http-interceptors-loggers"

externalUrl: “https://www.google.com” http by Dart Team

graph TD A[Christmas] -->|Get money| B(Go shopping) B --> C{Let me think} B --> G[/Another/] C ==>|One| D[Laptop] C -->|Two| E[iPhone] C -->|Three| F[Car] subgraph Section C D E F G end
graph TD A[UI] -->|User interaction| B(Event) B --> C{Bloc} C -->|Event| D[Repository] D -->|Response| C C -->|State| A

In this post, I’ll show you how to use the http package in Dart to make HTTP requests and how to use interceptors to refresh tokens and retry failed requests, everything while easily logging the requests and responses.
This post was made using:

http version 0.12.0+2 retry version 1.1.0 logging version 0.11.3+2 What is http? 🧐 The http package in Dart is a standard library for making HTTP requests. It supports GET, POST, PUT, PATCH and DELETE requests, and it can be used with or without interceptors, making it easy to modify requests and responses.

Concepts we will be covering in this post:

Interceptors Interceptors are a way to intercept and modify HTTP requests before they are sent to the server and to intercept and modify HTTP responses before they are returned to the caller. Loggers A logger is a way to log the requests and responses to the console. Retries A retry is a way to retry a failed request. Token and Refresh Token A token is a way to authenticate a user and a refresh token is a way to refresh that token when it expires. Setting up the http package 🔧 To use the http package, you need to add it to your pubspec.yaml file:

yaml Copy code dependencies: http: ^0.12.0+2 retry: ^1.1.0 logging: ^0.11.3+2 The retry and logging packages are optional, but I’ll be using them in this post, as they provide an easy way to log the requests and responses and to retry failed requests, but we will make our own retry logic for the refresh token part.
I also make use of flutter_appauth and flutter_secure_storage in the example found in the repo.

Making our custom HTTP client 🚀 To make a request, you need to create a Client instance and use the get method to make a GET request:

Author
Carlos Gutiérrez