Skip to content

ClientException: Failed to parse header value, when using http.post request #60142

Open
@andreykuzovlevv

Description

@andreykuzovlevv

I'm trying to make a secure HTTP request to a server, but it's failing to parse headers

I've tried both the standard HTTP client and Dio, with different header configurations like:

  final headers = {
    'Authorization': 'Bearer $token',
    'Content-Type': 'application/json',
  };

and

  final headers = {
    HttpHeaders.authorizationHeader: 'Bearer $token',
    HttpHeaders.contentTypeHeader: 'application/json',
  };

tried to trim it and utf8.encode(idToken)
but nothing seems to work

full code:

// Get the current user's ID token
  final idToken =
      await firebase_auth.FirebaseAuth.instance.currentUser?.getIdToken();
  if (idToken == null || idToken.trim().isEmpty) {
    throw Exception('User not authenticated');
  }

  print("idToken: $idToken");

  final token = idToken.trim().replaceAll('\n', '');
  final headers = {
    HttpHeaders.authorizationHeader: 'Bearer $token',
    HttpHeaders.contentTypeHeader: 'application/json',
  };

  print(headers);

  final body = jsonEncode({
    ...
  });

  try {
    final response = await http.post(
      url,
      headers: headers,
      body: body,
      encoding: Encoding.getByName('utf-8'),
    );

I/flutter ( 9250): {authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IjhkMjUwZDIyYTkzODVmYzQ4NDJhYTU2YWJhZjUzZmU5NDcxNmVjNTQiLCJ0eXAiOiJKV1QifQ.eyJwcm92aWRlcl9pZCI6ImFub255bW91cyIsImlzcyI6Imh0dHBzOi8vc2VjdXJldG9rZW4uZ29vZ2xlLmNvbS92b2NhYi04MGU1ZCIsImF1ZCI6InZvY2FiLTgwZTVkIiwiYXV0aF90aW1lIjoxNzM5NTk2OTI0LCJ1c2VyX2lkIjoiY0t1UHNrSE9DOGJSMGpGQVZLMWl1UFA4M1FEMyIsInN1YiI6ImNLdVBza0hPQzhiUjBqRkFWSzFpdVBQODNRRDMiLCJpYXQiOjE3Mzk2MDU1MzUsImV4cCI6MTczOTYwOTEzNSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6e30sInNpZ25faW5fcHJvdmlkZXIiOiJhbm9ueW1vdXMifX0.KtYS-d2beCFtzVmz2zrduieA47npgFHfCAWq7nNq1qmmr3Vuh-0cOQHeRv-btIBg34ux2t59Bx4tQcyM5tcQL3R2nROHeMGIQj0PIjrVr0QNy8NdLeq1KWK_9l2iqx3tTtkSZSrkVliUfC7biRe_YwojkhbUoLD8ZfeYJNqsCFWc-KaGDPjyAGfzgwOXMAX_-e3q2DR8L5vX05GTHXY6szHO_el0jib7OhxA9ZaMcArhycHUaxA5rCPgrCjwuQAeRIS2tN6KbkL1guqTv1AsNDhzKZXWi5DW8PySRY2lFUrIesknbFK8NUJEXYyd50nTp_TWqS0kyTKbGlqFX6L1_A, content-type: application/json; charset=UTF-8}

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

  • Dart 3.6.1 (stable) (Tue Jan 7 09:50:00 2025 -0800) on "windows_x64"
  • on windows / "Windows 10 Pro" 10.0 (Build 26100)
  • locale is en-US

Windows 11
Android Emulator

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-_http

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions