【问题标题】:flutter http.post is giving 500 error, while the api is working fine in postman颤振 http.post 给出 500 错误,而 api 在邮递员中工作正常
【发布时间】:2020-05-19 14:00:10
【问题描述】:

此登录 API 在邮递员中运行良好,但我在颤振中收到 500 状态代码错误。

下面是调用api的服务代码:

import 'dart:convert';
import 'package:http/http.dart' as http;
...

class AuthService {
  static const API_URL =
      "http://ec2-15-185-73-129.me-south-1.compute.amazonaws.com";
  static const headers = {
    'Content-Type': 'application/json'
  };
  Future<APIResponse<AuthResponse>> signIn() async {
    var body = jsonEncode({"username": "username", "password": "password"});
    var response =
        await http.post(API_URL + "/api-token-auth", headers: headers, body: body)));
    print(response.body.toString());
    print(response.request.toString());
    if (response.statusCode == 200) {
      // not reaching here
    } else {
      print(handleError(response.statusCode));
      // response.statusCode is **500**

    }
  }
}

print(response.body.toString()) 给出以下输出:

<!DOCTYPE html>
I/flutter ( 6158): <html lang="en">
I/flutter ( 6158): <head>
I/flutter ( 6158):   <meta http-equiv="content-type" content="text/html; charset=utf-8">
I/flutter ( 6158):   <meta name="robots" content="NONE,NOARCHIVE">
I/flutter ( 6158):   <title>RuntimeError
I/flutter ( 6158):           at /api-token-auth</title>
I/flutter ( 6158):   <style type="text/css">
I/flutter ( 6158):     html * { padding:0; margin:0; }
I/flutter ( 6158):     body * { padding:10px 20px; }
I/flutter ( 6158):     body * * { padding:0; }
I/flutter ( 6158):     body { font:small sans-serif; background-color:#fff; color:#000; }
I/flutter ( 6158):     body>div { border-bottom:1px solid #ddd; }
I/flutter ( 6158):     h1 { font-weight:normal; }
I/flutter ( 6158):     h2 { margin-bottom:.8em; }
I/flutter ( 6158):     h3 { margin:1em 0 .5em 0; }
I/flutter ( 6158):     h4 { margin:0 0 .5em 0; font-weight: normal; }
I/flutter ( 6158):     code, pre { font-size: 100%; white-space: pre-wrap; }
I/flutter ( 6158):     table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
I/flutter ( 6158):     tbody td, tbody th { vertical-align:top; padding:2px 3px; }
I/flutter ( 6158):     thead th {
I/flutter ( 6158):       padding:1px 6px 1px 3px; background:#fefefe; text-align:left;
I/flutter ( 6158):       font-weight:normal; font-size:11px; border:1px solid #ddd;
I/flutter ( 6158):     }
I/flutter ( 6158):     tbody th { width:12em; text-align:right; colo

print(response.request.toString()) 给出以下输出:

POST http://ec2-15-185-73-129.me-south-1.compute.amazonaws.com/api-token-auth

请帮忙

【问题讨论】:

  • 你能出示你的邮递员请求吗?
  • 当然。编辑了我的问题。
  • 我自己也遇到过类似的问题,发现是url打错了。你能仔细检查一下它实际上是你需要使用的正确的网址吗? (仔细检查不会有什么坏处)。
  • @ArtherEKing。三重检查

标签: http flutter


【解决方案1】:

替换网址:

API_URL + "/api-token-auth"

API_URL + "/api-token-auth/"

有点奇怪。您的服务器端路由可能有问题,但尾随 / 可以正常工作。

【讨论】:

  • @MuhammadAmir 很高兴能提供帮助。
【解决方案2】:

500 状态码表示服务器遇到了意外情况,导致无法完成请求。 请检查网址 或在 url 后添加 \ ,如下所示:

 var response =
    await http.post(API_URL + "/api-token-auth\", headers: headers, body: body)));

【讨论】:

  • 你不能在字符串结尾添加“\”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-18
  • 2020-08-27
  • 2020-08-07
  • 1970-01-01
  • 2021-10-08
  • 2021-11-01
相关资源
最近更新 更多