1

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?

2
  • 1
    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 Commented Jan 23, 2020 at 23:36
  • 1
    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 Commented Jan 24, 2020 at 19:09

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.