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? Any insights, code samples, or alternative approaches would be immensely helpful. Thank you!
********** Edit **********
Get with body work 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()
2
Answers
For Future reader
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.You cannot (or at least should not) send a body with a get request see. The http package cannot do it see and dio aswell see.
If you want to send data with a get request, you should use query parameters.