使用 Tauri 框架内置的 fetch 方法调用 GitHub API 时,出现 Request forbidden by administrative rules 错误。
GitHub API 要求所有的请求都必须包含一个有效的 User-Agent 头部。User-Agent 头部用于标识发起请求的用户或应用程序。
github 官网说明:
All API requests must include a valid User-Agent header. The User-Agent header identifies the user or application that is making the request. By default, GitHub CLI sends a valid User-Agent header. However, GitHub recommends using your GitHub username, or the name of your application, for the User-Agent header value. This allows GitHub to contact you if there are problems.
'User-Agent': 'Tauri-fetch' 即可解决此问题,Tauri-fetch 可替换为真实的,也可以自定义其他值。欢迎访问:天问博客