Reputation: 673
I was trying to get a response from api(i made this with node js and mongo db)
When I check the api with postman it gives me random outputs for different emails
Here for this email it give me correct output in postman
The problem is when I call this api in flutter it always gives me "new user" for every email I try
This is the code I used in flutter
class FbLoginService {
static Future<bool> FbAuth(body) async {
final stest = {"email": "[email protected]"};
final response = await http.post(
'http://example.com/api/user/loginwithfb',
body: stest
);
final data = response.body;
print(data);
// final temp = jsonEncode(data);
// Map<String, dynamic> user = jsonDecode(data);
// print(user['loginstatus']);
// if (user['loginstatus'] == 'newuser') {
// return true;
// } else {
// return false;
// }
return true;
}
}
This always gives me this out put
Upvotes: 0
Views: 223
Reputation: 673
I found that postman send this request with Content-Type':'application/json'
header
So adding that header to my code solved the problem and then it worked like a charm
headers = {'Content-Type':'application/json'}
final response = await http.post(
'http://example.com/api/user/loginwithfb',
body: stest,
headers:headers
);
Upvotes: 0
Reputation: 51770
In Postman you are sending a string as the body that looks like JSON.
in Dart you are passing a Map<String, String>
as body
, so the client will url form encode that.
To make your Dart code behave the same as Postman change:
body: stest,
to:
body: json.encode(stest),
Upvotes: 2