Headers not being set on http Flutter

2,986

Just dropping this here because this question does in fact has an answer. The comment left by Richard Heap lead me to the fix for this issue. Credits go to him, but I think it's best that this gets resolved so people who have this issue in the future won't need to go through the same search as I did.

My bet is that your server is (incorrectly) expecting Authorization to begin with a capital. However, Dart will lower case it. Test sending authorization using, say, Postman to confirm. If so, look at pub.dev/packages/alt_http

This does fix the issue. I didn't have issues with the key being 'Authorization' instead of 'authorization', but with the value containing 'Bearer' instead of 'bearer'. I validated this with the same approach:

Make a postman call exactly the same as your flutter app does it at the moment -> won't work.

Make a postman lowercase call -> fixes issue.

Share:
2,986
Kinetic
Author by

Kinetic

Updated on December 17, 2022

Comments

  • Kinetic
    Kinetic 11 months

    I'm building an app using Flutter and, while trying to utilize our api to access DB data, I get an error 400 saying that no headers were set in the request.

    I've tried multiple syntaxes from docs and other people's questions, but it never seems to work.

    Snippet for testing:

    Future<String> getList() async {
    
        String url = 'https://api.something/test';
    
        Map<String, String> headers = {"Authorization": "Bearer $test"};
    
        var res = await http.post(url, headers: headers);
    
        print('Response status: ${res.statusCode}');
        print('Response body: ${res.body}');
    
        return 'Finished';
      }
    

    I've tried using all of these and the result is always the same, no headers set.

        Map<String, String> headers = {"Authorization": "Bearer $test"};
    
        var res = await http.post(url, headers: headers);
    
        var res = await http.post(url, headers: {"Authorization": "Bearer $test"});
    
        var res = await http.post(url, headers: {HttpHeaders.authorizationHeader: "Bearer $test"};
    

    The question is, am I missing something? Is anything wrong with the code? I figured it was a back-end problem but we can't seem to fix it, so I just wanted to make sure that this should be working.

    I also tried to find some other api to test with but all the ones that have headers are for apitokens that you need to create an account for... Any suggestions on ones that I can debug with?

    • Richard Heap
      Richard Heap almost 4 years
      My bet is that your server is (incorrectly) expecting Authorization to begin with a capital. However, Dart will lower case it. Test sending authorization using, say, Postman to confirm. If so, look at pub.dev/packages/alt_http
    • Kinetic
      Kinetic almost 4 years
      You were right! Went to Postman like you said and the same error occurred, talked to our server people and now it works! Thanks man