You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用注册时出现308永久重定向问题求助

解决Flutter向Next.js(Vercel部署)发送POST请求返回308重定向的问题

问题原因

Vercel部署的Next.js应用默认强制HTTPS访问,当你使用HTTP协议发起请求时,Vercel会返回308永久重定向到对应的HTTPS地址,这就是你收到“Redirecting...”响应的原因。

解决方案

1. 直接使用HTTPS发起请求

将请求URL替换为HTTPS版本,这是最直接有效的解决方式,能避免额外的重定向请求:

Future<void> signup(String name, String email, String password) async {
    setLoading(true);
    try {
      var headersList = {
        'Accept': '*/*',
        'Content-Type': 'application/json'
      };
      // 修改为HTTPS URL
      var url = Uri.parse('https://taskmasterapp.vercel.app/api/appsignup');

      var body = {
        "name": name,
        "email": email,
        "password": password
      };

      http.Response response = await http.post(
        url,
        headers: headersList,
        body: jsonEncode(body)
      );
      String data = response.body;
      print(data);
    } catch (e) {
      print(e.toString());
    } finally {
      // 确保异常时也能关闭加载状态
      setLoading(false);
    }
  }

2. 显式开启重定向跟随(可选)

如果因特殊需求必须使用HTTP,可以显式设置重定向相关配置(http包默认已开启该功能),让请求自动跟随重定向:

http.Response response = await http.post(
  url,
  headers: headersList,
  body: jsonEncode(body),
  followRedirects: true,
  maxRedirects: 5,
);

注意:308状态码属于保留请求方法的重定向,重定向后依然会使用POST方法,但直接使用HTTPS可以减少一次请求开销,更推荐这种方式。

内容的提问来源于stack exchange,提问作者codingwizard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 00:02:19