What are the default headers used by http package?

Issue

What headers does Dart’s http package use by default when POST or GET requests made from a Flutter application? Does using a Client make difference?

Solution

I was curious this question myself, so I set up a little client-server example.

From a Simple dart file: I made a get request to my server (also written in Dart using shelf_plus(https://pub.dev/packages/shelf_plus) Highly recommend)

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

void main() async {
  Uri url = Uri.parse('http://localhost:1000/.well-known/version');
  http.Response response = await http.get(url);
  print('Response status: ${response.statusCode}');
  print('Response body: ${response.body}');
}

From the Server I logged the headers to my console and here was the response for the [GET] Request:

{
  user-agent: Dart/2.16 (dart:io), 
  accept: application/json, 
  accept-encoding: gzip, 
  host: localhost:1000
}

Next, I made a simple POST request:

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

void main() async {
  Uri url = Uri.parse('http://localhost:1000/.well-known/version');
  http.Response response = await http.post(url, body: {'hello': 'world'});
  print('Response status: ${response.statusCode}');
  print('Response body: ${response.body}');
}

Heres the headers on the server for the [POST] request:

{
  user-agent: Dart/2.16 (dart:io), 
  content-type: application/x-www-form-urlencoded; charset=utf-8, 
  accept-encoding: gzip, 
  content-length: 11, 
  host: localhost:1000
}

Hope this helps:)

Happy Coding

p.s if you want a place to save some of these useful code snippets take a look at Pieces(https://code.pieces.app/)

Answered By – Mark Widman

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *