在 flutter 中通过 http.get() 请求发送 json 数据时遇到问题

having trouble sending json data via http.get() request in flutter

我正在做一个 flutter 项目,我正在尝试调用基于 django 的 web-api,以某种方式检索与用户相关的帖子。现在我的 web-api 在 flutter app 之外测试时工作正常。 我可以登录、注册,因此我的连接没有问题。 有一个端点

.com/api/allBlogs/

此端点需要 json 数据,{"uid": any_integer}

现在我的问题是

how can I call http.get() request in flutter and send uid along with the request.

这是我尝试访问该端点以检索博客的代码。

  Future<void> loadPosts(String uid) async {
    final queryParameters = {
      'uid': uid,
    };
    final headers = {HttpHeaders.contentTypeHeader: 'application/json'};
    final url = Uri.https(
        'beautyexperience.herokuapp.com', 'api/allBlogs/', queryParameters);
    try {
      List<Post> _tempPosts = [];
      final response = await http
          .get(
        url,
        headers: headers,
      )
          .then(
        (value) {
             .
             .
             .
    } catch (error) {
      print('**************************************************************');
      print('error: $error');
      throw (error);
    }
    //return [..._posts];
  }

这是我收到的错误信息 错误

W/DynamiteModule(10228): Local module descriptor class for providerinstaller not found.
I/DynamiteModule(10228): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller(10228): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
I/flutter (10228): inside pickup_layout > build > streamBuilder > builder: line 25
I/flutter (10228): **************************************************************
I/flutter (10228): error: FormatException: Unexpected character (at character 1)
I/flutter (10228): <!DOCTYPE html>
I/flutter (10228): ^
E/flutter (10228): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter (10228): <!DOCTYPE html>
E/flutter (10228): ^
E/flutter (10228):
E/flutter (10228): #0      Posts.loadPosts
package:fyp_test/…/postsFolder/Posts.dart:110
E/flutter (10228): <asynchronous suspension>
E/flutter (10228):

更新

这是响应正文中返回的内容,即 value.body

I/flutter (11594): extractedData: <!DOCTYPE html>
I/flutter (11594): <html lang="en">
I/flutter (11594): <head>
I/flutter (11594):   <meta http-equiv="content-type" content="text/html; charset=utf-8">
I/flutter (11594):   <meta name="robots" content="NONE,NOARCHIVE">
I/flutter (11594):   <title>KeyError
I/flutter (11594):           at /api/allBlogs/</title>
I/flutter (11594):   <style type="text/css">
I/flutter (11594):     html * { padding:0; margin:0; }
I/flutter (11594):     body * { padding:10px 20px; }
I/flutter (11594):     body * * { padding:0; }
I/flutter (11594):     body { font:small sans-serif; background-color:#fff; color:#000; }
I/flutter (11594):     body>div { border-bottom:1px solid #ddd; }
I/flutter (11594):     h1 { font-weight:normal; }
I/flutter (11594):     h2 { margin-bottom:.8em; }
I/flutter (11594):     h3 { margin:1em 0 .5em 0; }
I/flutter (11594):     h4 { margin:0 0 .5em 0; font-weight: normal; }
I/flutter (11594):     code, pre { font-size: 100%; white-space: pre-wrap; }
I/flutter (11594):     table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }

您可以使用 Dio 打包并将您的数据作为 json 在 queryParameter 中发送。这里我举个例子

Response response;
var dio = Dio();
response = await dio.get('https://beautyexperience.herokuapp.com/api/allBlogs',
queryParameters: {"uid": any_integer});

更新 正如我发现的那样,您遇到了服务器错误。当您调用 api 时,服务器 returns 给您一个 html 响应,表示发生了错误,错误是 <title>KeyError at /api/allBlogs/</title>。可以检查您的 api 服务。

反正我建议用Dio更好更舒服