Flutter http.response doesn't get any response - android

i'm trying to get a simple response from http.Response. My code was working a few days ago and the only change from then to now was my location.
[...]
http.Response resp = await http.get(
Uri.encodeFull('$apiUrl/tblColMain'),
headers: {
"Accept": "application/json"
}
);
print(resp);
var data = json.decode(resp.body);
print(data);
This code doesn't print anything and shows no errors, I don't know what I'm doing wrong.
Thanks for any help!

Related

Flutter API Post Method Can not Send Data to SQL server localhost. Show Error "HTTP Error 400. The request hostname is invalid"

This is My Flutter Code--
void send() async
{
var bb=json.encode(listModel.toJson());
var response = await http.post(Uri.parse(
"http://10.0.2.2:1066/api/Storedata/Add"),
body: bb,
headers: {"Content-type": "application/json"},);
if (response.statusCode == 201)
{
print("Done");
}
else
{
print(response.body);
print(response.statusCode);
}
}
In "bb" variable all data show in json format when i debugged. but show this error "HTTP Error 400. The request hostname is invalid". Please Help!!!!

Api is working in postman but response always return 500 in flutter

My rest api is working successfully. When I send post request in flutter with Dio. Service always return 500 internal server error.
header
post request
Dio Options
To create a form data use this
var formData = FormData.fromMap({
'user': 'username',
'pass': 'password',
});
response = await dio.post('apiendpoint', data: formData);
I think you are missing the content-type in your header.. based on what your remote accepts either 'application/x-www-form-urlencoded' or 'application/json'
var data = {"phone": mobileNumber, "password": password};
var dio = Dio();
dio.options.headers['content-Type'] = 'application/x-www-form-urlencoded';
try {
var response = await dio.post(ApiUrl.baseUrl + url, data: data);
print(response);
} on DioError catch (e) {
print(e);
}
you can try this way :
var formData = {
'user': 'username',
'pass': 'password',
};
response = await dio.post('apiendpoint', data: jsonEncode(formData));

Flutter: response on http post returns empty

I'm trying to get the response after making a post to an api, but the response (which is a json) doesnt come, when i try to print the line is empty.
Can someone help me to figure out what i'm making wrong?
I have printed the status code just for testing and its returning 500, could this be related?
Future<void> _login() async {
Map<String, dynamic> newLogin = Map();
newLogin["user"] = _usuarioController.text.trimLeft();
newLogin["pass"] = _senhaController.text.trimLeft();
Map<String, String> headers = new Map<String, String>();
headers["Content-type"] = "application/json";
headers["Accept"] = "application/json";
int timeout = 2;
http.Response response = await http
.post('https://sistema.hutransportes.com.br/api/login.php',
headers: headers, body: jsonEncode(newLogin), encoding: utf8)
.timeout(Duration(seconds: timeout));
print(newLogin);
print(response.statusCode);
print(response.body); //Where the empty response comes
}

why is my flutter app sending unlimited request

while trying to get the list of countries from my api, my code is sendig unlimited request to the server when i call the function one time. here is the flutter code
Future<dynamic> listePays() async{
http.Response response = await http.get(apiUrl+"api_pays", headers: {"Accept": "application/json"});
var resp = json.decode(response.body);
return resp;
}
i want to send just one request and work with the answers. ihave tried removing Future but i get the same issue
i call the function like this:
getPays() async {
Functions().listePays().then((data) async {
pays = data;
setState(() {});
});
}

flutter - no json is sent in the body of my http post

this is the code.
Future<http.Response> postRequest () async {
var url ='http://10.0.2.2:3000/api/message';
Map data = {
'message': '12345678901234567890'
};
//encode Map to JSON
var body = json.encode(data);
var response = await http.post(url,
headers: { "accept": "application/json", "content-type": "application/json" },
body: body
);
print("${response.statusCode}");
print("${response.body}");
return response;
}
postRequest();
// also tried this: headers: {"content-type":"application/json" },
In my python flask server, the post message is logging, but with empty body into it.
The flutter app is running on Android Virtual Device and the server is still running on http://0:0:0:0:3000/ and it's using request.get_json() in the method.
Using postman, everything works as expected on my server so I see the problem in the app.
postman details:
POST: http://localhost:3000/api/message
headers
KEY | VALUE
Content-Type | application/json
Body raw
{
"message": "opa"
}
also raised here: https://github.com/flutter/flutter/issues/39351
Try passing :
Future<http.Response> postRequest () async {
var url ='http://10.0.2.2:3000/api/message';
Map<String, String> data = { "message": "opa" };
var body = json.encode(data);
var response = await http.post(url,
headers: { "accept": "application/json", "content-type": "application/json" },
body: body
);
print(response.statusCode);
print(response.body);
return response;
}
postRequest().then((response){
print(response.body);
});
not sure if my finding is precious or not for community, but it seems that the URL was the problem. Everything works now, but I added a / in the end of the string in my flask app.
In Postman, I didn't have to do this. So if you have same problem, take care :)
The log from Flask server:
127.0.0.1 - - [30/Aug/2019 17:41:32] "POST /api/message/ HTTP/1.1" 200 -
opa
My Flask url is:
#app.route('/api/message/', methods=['POST'])
def function():

Categories

Resources