flutterdarthttpflutter-http

Flutter web app encounters error when sending GET request with body


I'm currently developing a Flutter web application where I've encountered an issue specifically when attempting to send a GET request with a body. The application works as expected on Chrome, but upon sending this particular request, I consistently receive an error indicating that the body is empty.

I've tried using both the HTTP and Dio packages, and I've also added the Access-Control-Allow-Origin header to handle cross-origin issues, but the problem persists.

Is there a known workaround or solution to successfully send GET requests with bodies in Flutter web apps?

Get with body works in Android and iOS:

//sample code
Map<String, String> header = {};

String? token = SharedPrefsHelper.getString(prefToken);

String langCode = SharedPrefsHelper.getString(prefLanguageCode) ?? 
 "ar";

header['content-type'] = 'application/json';
header["Authorization"] = "bearer $token";
header["Accept-Language"] = langCode;

final request = Request('GET', 
Uri.parse(url))..headers.addAll(header);
request.body = json.encode(parameter);
final response = await request.send()

Solution

  • For Future reader

    Some security mechanisms, such as web application firewalls (WAFs) or proxies, might be configured to block or modify GET requests with bodies due to security concerns.

    If you are trying to send data with a request, especially if it involves a request body, it's more appropriate to use a POST request. The POST method is designed for submitting data to be processed to a specified resource.