如何正确查询GitHub API URL?

huangapple go评论104阅读模式
英文:

How to query github api url properly?

问题

我想制作一个Flutter应用程序,使用"Flutter"关键字搜索GitHub并显示前50个最受欢迎的存储库。

我尝试的内容是----
https://api.github.com/这个URL包含所有可用的URL列表。因此,我从这个列表中取了https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}这个URL。
当我将这个URL直接在Postman中使用时,我只得到了29个结果,即使我将页数设置为1,我仍然得到29个结果,即使我将页数设置为2并且每页数量设置为25,我仍然得到29个结果。

请帮忙,我需要50个结果。

英文:

I want to make a Flutter app that uses the "Flutter" keyword to search GitHub and displays the top 50 most popular repositories.

What I tried ----
https://api.github.com/ this url has list of all available urls. Hence, from this list I took, https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order} this url.
when I use this url just how it is inside of post man, I only get 29 results and if I set the page to 1 I still get 29 results and even if I set page = 2 and per_page = 25, I still get 29 results.

Please help, I need 50 results.

答案1

得分: 1

你必须使用 per_page 参数。

https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28

如何正确查询GitHub API URL?

https://api.github.com/search/repositories?q={query}&per_page=50

英文:

You have to use the per_page parameter.

https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28

如何正确查询GitHub API URL?

https://api.github.com/search/repositories?q={query}&per_page=50

答案2

得分: 0

请确保在您的Flutter项目中包含必要的依赖项。您需要使用http包进行HTTP请求。将其添加到您的pubspec.yaml文件中:

dependencies:
  flutter:
    sdk: flutter
  http: ^0.13.3

创建一个新的Dart文件,例如github_api.dart,并导入必要的包:

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

定义一个函数,用于从GitHub API检索存储库:

Future<List<dynamic>> fetchRepositories() async {
  final url = Uri.parse('https://api.github.com/search/repositories?q=flutter&per_page=50');
  final response = await http.get(url);

  if (response.statusCode == 200) {
    final Map<String, dynamic> data = json.decode(response.body);
    return data['items'];
  } else {
    throw Exception('Failed to fetch repositories');
  }
}

这就是您要的翻译内容,没有其他内容。

英文:

> Make sure you include the necessary dependencies in your Flutter project. You'll need the http package for making HTTP requests. Add it to your pubspec.yaml file:

dependencies:
 flutter:
 sdk: flutter
 http: ^0.13.3

> Create a new Dart file, such as github_api.dart, and import the necessary packages:

import &#39;package:http/http.dart&#39; as http;
import &#39;dart:convert

> Define a function that will retrieve the repositories from the GitHub API:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

Future&lt;List&lt;dynamic&gt;&gt; fetchRepositories() async {
  final url = Uri.parse(&#39;https://api.github.com/search/repositories?q=flutter&amp;per_page=50&#39;);
  final response = await http.get(url);

  if (response.statusCode == 200) {
    final Map&lt;String, dynamic&gt; data = json.decode(response.body);
    return data[&#39;items&#39;];
  } else {
    throw Exception(&#39;Failed to fetch repositories&#39;);
  }
}

huangapple
  • 本文由 发表于 2023年6月18日 20:38:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定