I wrote simple http server that accept get request and return data. Data are accessable from browser.
The problem with Flutter app. I can't get result in: http.get().then
when I am trying to get access to my local web server. But When I am changing it to public it's work.
Code:
class HomeView extends StatelessWidget {
@override
build(Build) {
return Column(
children: [
WorkersSlider(),
RaisedButton(onPressed: () => {
print("sdfsfd"),
http.get('https://my-json-server.typicode.com/typicode/demo/posts',
// http.get('http://127.0.0.1:5000/status',
headers: {
"Accept": "application/json",
"Access-Control_Allow_Origin": "*"
}
).then((value) => {
print("1111113"),
print("value from parser: $value")
}),
} ),
],);
}
}
server code:
main() async {
const int PORT = 5000;
var app = express();
app.use(BodyParser.json());
app.get('/status', (req, res) async {
print("status request");
List jobsAsList = [];
for(var job in jobsList)
{
// jobs generation logic is skipped
jobsAsList.add(job.toJson());
}
res.json({'data': jobsAsList});
});
app.listen(port: PORT, cb: (int port) => print('Listening on port $port'));
}
curl works:
curl -X GET http://127.0.0.1:5000/status
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 279 0 279 0 0 272k 0 --:--:-- --:--:-- --:--:-- 272k{"data":[{"jobPid":13772,"job_cmd":null,"jobStatus":null,"lastPingDate":"2021-01-25T14:53:47.000"},{"jobPid":8544,"job_cmd":null,"jobStatus":null,"lastPingDate":"2021-01-25T14:53:47.000"},{"jobPid":18156,"job_cmd":null,"jobStatus":null,"lastPingDate":"2021-01-25T14:53:47.000"}]}
Any ideas?
UPD: https://github.com/dart-lang/http/issues/433
question from:
https://stackoverflow.com/questions/65885624/impossible-to-get-answer-from-local-http-server 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…