Joshua Beckers
Joshua Beckers

Reputation: 907

Flutter/Dart http POST redirect send body via GET

I'm trying to POST data to a Google Sheets web app.

When I POSTdata to the endpoint I get a status 302 back. If I then POST the data to the new location I get a status 405.

It works when I use Postman but I can't get it to work in Flutter.

Postman receives the 302 and then sends a GET request with my JSON payload as body. This seems to be against the GET specification but it works.

I tried the flutter HTTP package and DIO, both don't let me send a body via GET. I tried DIO with and without followRedirects.

How can I send a body via GET in flutter?

Upvotes: 1

Views: 1821

Answers (2)

Pavel Shastov
Pavel Shastov

Reputation: 3047

you can use dio request instead

await dio.request(
  "/test",
  data: {"id": 12, "name": "xx"},
  options: Options(method: "GET"),
);

Upvotes: 4

Milvintsiss
Milvintsiss

Reputation: 1460

With dio configure your Options widget like following to avoid redirection for the first request:

Options(
        followRedirects: false,
        validateStatus: (status) {
          return status < 500;
        }),

GET is not intended to send data, please see https://stackoverflow.com/a/60312720/10286880.

If you want to send them with POST you can do the following:

final res = await dio.post(
    '[YOUR ADRESS]',
    data: json.encode([YOUR BODY AS MAP])
  );

Upvotes: 0

Related Questions