I am trying to connect Flutter's HttpClient to fetch data from my local server which is running on ASP.Net Core 3.0. The problem is I get the Error 400(Bad Request) each time I try.
Here's the flutter code:
String token = await SharedPreferencesService.getStringFromSF('idToken');
var client = new HttpClient();
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
HttpClientRequest request =
await client.getUrl(Uri.parse('https://10.0.2.2:44383/weatherforcast'));
request.headers.add('idToken', token);
HttpClientResponse response = await request.close();
String reply = await response.transform(utf8.decoder).join();
The asp.net core 3.0 endpoint:
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[HttpGet]
public User Get()
{
return new User { UserId = 1332, Username = "Michal" };
}
}
The Error
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>
I can confirm that I get the data in the browser as well as on postman. Thank you for the help.