1

I have a login page where i am trying to send a login request to my backend. But I get an Unhandled Exception: Invalid header field name. Here is my submit function

submit() async {
var res = await LoginAPI().loginData(
    {'email': _emailController.value, 'password': _passwordController.value});
var body = json.decode(res.body);
print(body);
}

Then in my LoginAPI class here is my loginData function that makes the call to the backend

import 'dart:convert';

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

class LoginAPI {
  final String _url = "http://10.0.2.2:8000/api/";
  Map<String, String> headers = {"Content-type": "application/json"};

 loginData(data) async {
 var fullUrl = _url + "v1/users/login";
  return await http.post(
    fullUrl, 
    body: jsonEncode(data),
    headers: headers
    );      
   }
  }

Here is my request through postman Postman request

Postman request 2

Here is my response through postman Response Image

When I make the same request with Postman i get the response I am supposed to get. What Am i doing wrong?

7
  • possible duplicate stackoverflow.com/questions/49163993/… Commented Nov 28, 2019 at 9:14
  • also it would be great if you share response Commented Nov 28, 2019 at 9:15
  • I have shared my response and that post did not help me at all. Commented Nov 28, 2019 at 9:21
  • You may be encountering a well known server bug, where the server expects a certain case of the header names. package:http lower cases all header names, which is perfectly valid but confuses some servers. Confirm with another client (not sure if you can control the header name in postman) with a lower case content-type. If you can duplicate the problem, then it's a server issue. You can always just use Dart's HttpClient which does not change the case of headers. Commented Nov 28, 2019 at 15:02
  • @RichardHeap thanks. So i sent a request with postman using lower case content-type and I got the right response back. I guess I will just use dart HttpCLient Commented Nov 29, 2019 at 8:09

3 Answers 3

1

try this

Map<String, String> headers = {"Content-type": "application/json",  "Accept": "application/json",};
Sign up to request clarification or add additional context in comments.

Comments

1

It looks from your postman request that you are just sending form data (not a json encoded body). package:http will form encode the body for you (and add the content type header) if you do the following:

  return await http.post(
    fullUrl, 
    body: data,
  ); 

Comments

0

So i was able to solve the issue. The issue was with my CORS middleware on my server. I just made some changes and it worked fine. So if anyone has this issue just know it has nothing to do with flutter but most probably CORS

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.