Dio: Sending a JSON body with GET request?

Created on 11 Apr 2019  路  6Comments  路  Source: flutterchina/dio

How would I go about sending a GET request with a JSON body, have tried the following, but the server recieves and empty response (example is simplified).

main() {
  var body = {
    'auth': json.encode({
      'phone': "12312312",
    })
  };

  var response = await _client.request("/api/token",
      data: json.encode(body),
      options: Options(method: "get", contentType: ContentType.json));
}
stale

Most helpful comment

Why this restriction? I have to deal with a server that wants the parameters in the request body and unfortunately, just because of that it means I have to use something else than Dio. It looks like a wrong design choice, as GET requests with a request body are becoming more and more popular.

I think you should at least let us do it even if you consider it a bad practice, because we may have no control over the server API.

All 6 comments

Only "POST", "PUT", "PATCH" methods are allowed with request body, GET request just use queryParameters.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

Why this restriction? I have to deal with a server that wants the parameters in the request body and unfortunately, just because of that it means I have to use something else than Dio. It looks like a wrong design choice, as GET requests with a request body are becoming more and more popular.

I think you should at least let us do it even if you consider it a bad practice, because we may have no control over the server API.

@Daimas @HelgeSverre i think you can edit dio.dart to obtain your feature. this is my edit, i hope you can try it.

there are two place need to edit in dio.dart.
the first part is (you can search over the comment "Handy method .....").
change to the those part like this. (just add data in parameter in both part)
````
/// Handy method to make http GET request, which is a alias of [BaseDio.request].
Future> get(
String path, {
data,
Map queryParameters,
Options options,
CancelToken cancelToken,
ProgressCallback onReceiveProgress,
});

` 

the **second part** is

`

/// Handy method to make http GET request, which is a alias of [BaseDio.request].
Future> get(
String path, {
data,
Map queryParameters,
Options options,
CancelToken cancelToken,
ProgressCallback onReceiveProgress,
}) {
return request(
path,
data: data,
queryParameters: queryParameters,
options: checkOptions("GET", options),
onReceiveProgress: onReceiveProgress,
cancelToken: cancelToken,
);
}
``

hope it help..

@azizainunnajib Thank you but actually I was already using request(data: ...) directly instead of get(), as it's the only way to pass a data parameter to a GET request. The problem is that the request body is empty, the data parameter is completely ignored.

After some digging I have found that you have to remove the second part of this if in dio.dart:

if (data != null &&
    ["POST", "PUT", "PATCH", "DELETE"].contains(options.method)) {

Now it works as expected. I would really prefer if it was not deliberately restricted, especially since it's such a simple change. It's painful to have to manage a copy of the repo just to modify that line. Other clients do not have such restrictions, or have removed them since GET requests with a request body are (unfortunately) becoming popular.

@Daimas i had never tried it. so i dont even know my solution is working or not. and maybe not working because of that if.
but it's really nice since you found solution of this problem.

Was this page helpful?
0 / 5 - 0 ratings