I have an HTTP(S) GET request which works fine in java (Android sdk) and Python, but when I tried to port it to flutter it returns an empty string. It should return a JSON with ~2MB of data in it.
Here is the Python code:
import http.client
HOST, PORT = 'kretaglobalmobileapi.ekreta.hu', 443
headers = {"apiKey": "7856d350-1fda-45f5-822d-e1a2f3f1acf0", "Connection": "keep-alive",
"Accept": "application/json", "HOST": "kretaglobalmobileapi.ekreta.hu"}
conn = http.client.HTTPSConnection(host=HOST, port=PORT)
conn.request("GET", "/api/v1/Institute", headers=headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
I have tried two different approaches for the Flutter version, none worked.
First approach:
import 'dart:async';
import 'dart:convert' show utf8, json;
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<http.Response> fetchPost() {
return http.get(
"https://kretaglobalmobileapi.ekreta.hu/api/v1/Institute",
// Send authorization headers to your backend
headers : {"apiKey": "7856d350-1fda-45f5-822d-e1a2f3f1acf0", "Connection": "keep-alive",
"Accept": "application/json", "HOST": "kretaglobalmobileapi.ekreta.hu"}
);
}
void login() async {
print((await fetchPost()).body);
print((await fetchPost()).body.length);
}
It returns:
Performing full restart...
Restarted app in 3,648ms.
I/flutter (18739):
I/flutter (18739): 0
Second:
Future<Map<String, dynamic>> getInstitutes(HttpClient client) async {
final String url = "https://kretaglobalmobileapi.ekreta.hu/api/v1/Institute";
final HttpClientRequest request = await client.getUrl(Uri.parse(url))
..headers.add("Accept", "application/json")
..headers.add("HOST", "kretaglobalmobileapi.ekreta.hu")
..headers.add("apiKey", "7856d350-1fda-45f5-822d-e1a2f3f1acf0")
..headers.add("Connection", "keep-alive");
final HttpClientResponse response = await request.close();
print(json.decode(await response.join()));
return json.decode(await response.transform(utf8.decoder).join());
}
I'm leaving out the GUI and boilerplate code here, the full code is on pastebin.
Update: The problem was, that the API I was connecting to, used outdated HTTP-standards. I told the support about this, they told me they aren't willing to fix it so I will have to create a new webserver (probably in Python) that will operate between the client and the API. Another option would be to use native Android and iOS code that will hopefully support case-sensitive headers. Huh, all this fuss is because they can't change a capital 'K' to a lowercase 'k'.