Search code examples
flutterhttp-postdart-htmlhttp-status-code-303

Flutter web not redirecting to server-side endpoint


I have created an endpoint in my server that has a status code of 303 and redirects to a prebuilt stripe checkout page. Though, when I post to this endpoint using this http package, my flutter app does not redirect to the checkout page. Instead it responds with a get request to the redirect location header with a status code of 200. The checkout page is never redirected to.

response = await http.post(Uri.parse('myURL'));

Trying to stop the post request from responding with a get request, I tried to catch the location header and redirect to it using this url-launcher package. But this throws an error because it is still redirecting to a get request of the checkout page link and there is not a location header.

      http.Request request = http.Request("post", Uri.parse('myURL'))..followRedirects=false..maxRedirects=0;
      http.Client baseClient = http.Client();
      http.StreamedResponse response = await baseClient.send(request);
      Uri redirectUri = Uri.parse(response.headers['location']!);
      if (!await launchUrl(redirectUri, mode: LaunchMode.externalApplication)) {
        throw 'Could not launch $redirectUri';
      }

If anyone has any ideas on how to have my flutter app redirect to the status code 303 location header link, I would appreciate the help. Thanks.


Solution

  • I never figured out how to get Flutter to redirect to a new page from a post request, but I came up with a different solution. Using the 'dart:html' package, I was able to use its window redirect option with a url. In my api I returned the url I wanted to redirect to through the body.

    String redirectUri = response.body;
    html.window.open(redirectUri, "_self");