I am trying to log into an API with flutter. Here is the method:
var result = await http.post(
Uri.https(host, url, queries),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'username': myUsername,
'password': myPassword,
}),
);
The request result with a 405 error saying:
This method requires HTTP POST
Please, how do I deal with that ?
Edit:
This seems to work:
Map<String, String> formMap = {
'username': 'myUsername',
'password': 'myPassword',
};
http.Response response = await http.post(
Uri.https(host, url, queries),
body: jsonEncode(formMap),
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
encoding: Encoding.getByName("utf-8"),
);
- Status code 200
- Response body = {"stat":"fail","err":1002,"message":"Missing parameters: username"}
Looks like the "body" of my request is not recognized by the server.