如何发送带有数组作为参数的 GET 请求?

里克桑特拉

我试图创建一个函数来使用查询参数进行 GET。我正在处理Mangadex API,并将一个名为“manga”的参数作为数组发送。我创建的代码如下:

Future<http.Response> getCoverArtResponse(String mangaID) async {
  var queryParameters = {
    'limit': '10',
    'manga': [mangaID] //Here
  };
  var unencodedPath = '/cover';
  var response = await http.get(
      Uri.https(authority, unencodedPath, queryParameters),
      headers: {HttpHeaders.contentTypeHeader: 'application/json'});
  return response;
}

但是,响应是以下错误:

{"result":"error","errors":[{"id":"9c346772-7b14-5982-b4b6-7b5888522762","status":400,"title":"validation_exception","detail":"Error validating \/manga: String value found, but an array is required","context":null}]}

我应该如何发送参数?到目前为止,我已经尝试过 -

'manga': [mangaID]
'manga': '[$mangaID]'

它们似乎都不起作用。

威廉·阿奇亚
import 'dart:async';
import 'package:wnetworking/wnetworking.dart';

class MangaDex {
  static const _base = 'https://api.mangadex.org';

  static FutureOr<void> _getter({required String url, required Function(JMap item, int idx) onItem}) async {
    await HttpReqService.getJson<JMap>(url)
      .then((response) {
        var results = response?['results'];
        if (results != null) {
          if (results is List) {
            var i = 0;
            results.forEach((manga) => onItem(manga, ++i));
          } else {
            print(response);
          }
        }
      });
  }

  static FutureOr<void> cover({int limit = 10, int offset=0, String? mangaId, String? coverId}) async {
    final mangas = mangaId != null ? '&manga[]=$mangaId' : '';
    final covers = coverId != null ? '&ids[]=$coverId' : '';
    final url = '$_base/cover?limit=$limit&offset=$offset$mangas$covers';

    await _getter(
      url: url, 
      onItem: (item, idx) {
        print('$idx) "${item['data']?['attributes']?['fileName']}"');
        print('   id: ${item['data']?['id']}\n');
      },
    );
  }
}

void main(List<String> args) async {
  await MangaDex.cover(mangaId: '32d76d19-8a05-4db0-9fc2-e0b0648fe9d0', limit: 2);
  print('\nJob done');
}

结果:

1) "f5873770-80a4-470e-a11c-63b709d87eb3.jpg"
   id: b6c7ce9c-e671-4f26-90b0-e592188e9cd6

2) "e9f926db-b469-48c4-8cc4-a8e523ad75ca.jpg"
   id: 00aae6e0-46bb-4f92-a82a-1c740789b704


Job done

wnetworking替换http,并JMapMap<String, dynamic>

注意:MangaDex 文档缺乏并误导了如何正确使用其端点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何发送带有某些参数的GET请求?

发送带有标头的HTTP GET请求

发送带有数据的请求

如何在GET调用中将地图作为请求参数发送

如何从PHP发送GET请求?

发送带有节点参数的http get请求

数组作为Postman中GET请求中的参数

在正文中发送GET请求参数

Ruby-发送带有标头的GET请求

将Ajax请求作为GET请求而不是PUT请求传递

使用JavaScript(XMLHttpRequest)发送带有正文的GET请求

如何使用JavaScript Fetch将get请求发送到带有字符串参数的jsonresult?

发送带有数据和JSON参数的发布请求

如何使用Python发送带有数据的获取请求?

在react js中,如何将带有参数的axios GET方法请求发送到Node js,在Node js中,如何获取这些参数

如何发送带有参数的邮递员Get-Request作为另一个请求的响应数组

如何通过带有Restangular的GET发送参数数组

AngularJS-发送带有关联数组参数的$ http GET请求

GET请求中的“ _”参数

从横幅发送GET请求

Volley GET 请求发送空参数

带有参数 Node.js 的 HTTP GET 请求

如何使用 JSON 数组作为参数调用 JAVA GET 请求

如何在 JMeter 中发送带有可变路径参数的 GET 请求?

在flutter中发送带有json正文的GET请求

如何使用请求 2.18 发送带有 JSON 正文的 GET 请求?

如何循环遍历包含键和值的数组以在 ReactJS 中发送对 Axios.get 参数的请求?

请求库Python中带有文件参数的GET请求

Vue.js - 如何发送带有 id 作为路径参数的发送 GET 请求