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?
Authorizationto begin with a capital. However, Dart will lower case it. Test sendingauthorizationusing, say, Postman to confirm. If so, look at pub.dev/packages/alt_http