cfug / dio

A powerful HTTP client for Dart and Flutter, which supports global settings, Interceptors, FormData, aborting and canceling a request, files uploading and downloading, requests timeout, custom adapters, etc.
https://dio.pub
MIT License
12.5k stars 1.51k forks source link

Validation Failed #560

Closed kimmy-wang closed 4 years ago

kimmy-wang commented 4 years ago

New Issue Checklist

Issue Info

Info Value
Platform Name flutter
Platform Version 1.9.1
Dio Version 3.0.4
Android Studio / Xcode Version Android Studio 3.5.1 / Xcode 11.2.1
Repro rate e.g. all the time (100%) / sometimes x% / only once
Repro with our demo prj e.g. does it happen with our demo project?

Issue Description and Steps

请求这个地址https://api.github.com/search/issues?q=repo:dragen1860/Deep-Learning-with-TensorFlow-book+is:pr+state:open 为什么进行了编码处理?

flutter:
flutter: ╔╣ Request ║ GET
flutter: ║  https://api.github.com/search/issues?q=repo%3Adragen1860%2FDeep-Learning-with-TensorFlow-book%2Bis%3Apr%2Bstate%3Aopen
flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════
flutter: ╔ Query Parameters
flutter: ╟ q: repo:dragen1860/Deep-Learning-with-TensorFlow-book+is:pr+state:open
flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════
flutter: ╔ Headers
flutter: ╟ content-type: application/json; charset=utf-8
flutter: ╟ Accept: application/vnd.github.v3+json
flutter: ╟ contentType: application/json; charset=utf-8
flutter: ╟ responseType: ResponseType.json
flutter: ╟ followRedirects: true
flutter: ╟ connectTimeout: 20000
flutter: ╟ receiveTimeout: 20000
flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════
flutter:
flutter: ╔╣ Response ║ GET ║ Status: 422 Unprocessable Entity
flutter: ║  https://api.github.com/search/issues?q=repo%3Adragen1860%2FDeep-Learning-with-TensorFlow-book%2Bis%3Apr%2Bstate%3Aopen
flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════
flutter: ╔ Body
flutter: ║
flutter: ║    {
flutter: ║         message: "Validation Failed",
flutter: ║         errors: [
flutter: ║            {
flutter: ║                 "The listed users and repositories cannot be searched either because the r
flutter: ║                 esources do not exist or you do not have permission to view them."
flutter: ║                 resource: "Search",
flutter: ║                 field: "q",
flutter: ║                 code: "invalid"
flutter: ║            }
flutter: ║         ],
flutter: ║         documentation_url: "https://developer.github.com/v3/search/"
flutter: ║    }
flutter: ║
flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════
flutter: Exception: Failed to request /search/issues.
wendux commented 4 years ago

你的请求代码贴一下

kimmy-wang commented 4 years ago

发送离两次请求:

第1次q的值:repo:upcwangying/githubjs+is:pr,报错 第2次q的值:repo:upcwangying/githubjs,正常

  1. dao
static Future<SearchRepository> fetchRepositories(
    String q, {
    RepositorySortTypes sort = RepositorySortTypes.stars,
    OrderTypes order = OrderTypes.desc,
    int page = 1,
    int pageSize = 10,
  }) async {
    if (q == null || q.isEmpty) {
      throw Exception("搜索内容不允许为空!");
    }

    try {
      Response response = await authHttp
          .get('/search/repositories', queryParameters: {'q': q,
        'sort': describeEnum(sort),
        'order': describeEnum(order),
        'page': page, 'per_page': pageSize});
      if (response.statusCode == 200) {
        var result = response.data;
        if (result != null) {
          return SearchRepository.fromJson(result);
        }
      }
    } on DioError catch (e) {
      print(e);
    }

    return null;
  }
  1. authHttp
    
    import 'package:dio/dio.dart';
    import 'package:dio_http_cache/dio_http_cache.dart';
    import 'package:provider_demo/http/http.dart';

final AuthHttp authHttp = AuthHttp();

class AuthHttp extends BaseHttp { @override void init() { } }

Options buildOptions(bool forceRefresh) { return buildCacheOptions(Duration(days: 7), forceRefresh: forceRefresh); }


3. BaseHttp

import 'dart:convert';

import 'package:dio/dio.dart'; import 'package:dio/native_imp.dart'; import 'package:dio_http_cache/dio_http_cache.dart'; import 'package:flutter/foundation.dart'; import 'package:pretty_dio_logger/pretty_dio_logger.dart'; import 'package:provider_demo/constants/constants.dart'; import 'package:provider_demo/http/interceptors/base_interceptor.dart';

var dio = Dio();

// 必须是顶层函数 _parseAndDecode(String response) { return jsonDecode(response); }

parseJson(String text) { return compute(_parseAndDecode, text); }

abstract class BaseHttp extends DioForNative { BaseHttp() { /// 初始化 加入app通用处理 (transformer as DefaultTransformer).jsonDecodeCallback = parseJson; interceptors ..addAll([ BaseInterceptor(), DioCacheManager(CacheConfig( baseUrl: Constants.GITHUB_API_PREFIX, databaseName: 'DioCache', defaultMaxStale: Duration(days: 10))) .interceptor, PrettyDioLogger( requestHeader: true, requestBody: true, responseBody: true, responseHeader: false, error: true, compact: true, maxWidth: 90, ) ]); init(); }

void init(); }


4. BaseInterceptor

import 'package:dio/dio.dart'; import 'package:provider_demo/constants/constants.dart';

class BaseInterceptor extends InterceptorsWrapper { @override Future onRequest(RequestOptions options) { options ..baseUrl = Constants.GITHUB_API_PREFIX ..connectTimeout = 5 1000 //5s ..receiveTimeout = 5 1000;; return super.onRequest(options); }

}


5. 两次请求日志

flutter: flutter: ╔╣ Request ║ GET flutter: ║ https://api.github.com/search/repositories?q=repo%3Aupcwangying%2Fgithubjs%2Bis%3Apr&sort=stars&order=desc&page=1&per_page=10 flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════ flutter: ╔ Query Parameters flutter: ╟ q: repo:upcwangying/githubjs+is:pr flutter: ╟ sort: stars flutter: ╟ order: desc flutter: ╟ page: 1 flutter: ╟ per_page: 10 flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════ flutter: ╔ Headers flutter: ╟ content-type: application/json; charset=utf-8 flutter: ╟ contentType: application/json; charset=utf-8 flutter: ╟ responseType: ResponseType.json flutter: ╟ followRedirects: true flutter: ╟ connectTimeout: 5000 flutter: ╟ receiveTimeout: 5000 flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════ flutter: flutter: ╔╣ DioError ║ Status: 422 Unprocessable Entity flutter: ║ https://api.github.com/search/repositories?q=repo%3Aupcwangying%2Fgithubjs%2Bis%3Apr&sort=stars&order=desc&page=1&per_page=10 flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════ flutter: ╔ DioErrorType.RESPONSE flutter: ║ { flutter: ║ message: "Validation Failed", flutter: ║ errors: [ flutter: ║ { flutter: ║ "The listed users and repositories cannot be searched either because the r flutter: ║ esources do not exist or you do not have permission to view them." flutter: ║ resource: "Search", flutter: ║ field: "q", flutter: ║ code: "invalid" flutter: ║ } flutter: ║ ], flutter: ║ documentation_url: "https://developer.github.com/v3/search/" flutter: ║ } flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════ flutter: flutter: DioError [DioErrorType.RESPONSE]: Http status error [422] flutter: flutter: ╔╣ Request ║ GET flutter: ║ https://api.github.com/search/repositories?q=repo%3Aupcwangying%2Fgithubjs&sort=stars&order=desc&page=1&per_page=10 flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════ flutter: ╔ Query Parameters flutter: ╟ q: repo:upcwangying/githubjs flutter: ╟ sort: stars flutter: ╟ order: desc flutter: ╟ page: 1 flutter: ╟ per_page: 10 flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════ flutter: ╔ Headers flutter: ╟ content-type: application/json; charset=utf-8 flutter: ╟ contentType: application/json; charset=utf-8 flutter: ╟ responseType: ResponseType.json flutter: ╟ followRedirects: true flutter: ╟ connectTimeout: 5000 flutter: ╟ receiveTimeout: 5000 flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════ flutter: flutter: ╔╣ Response ║ GET ║ Status: 200 OK flutter: ║ https://api.github.com/search/repositories?q=repo%3Aupcwangying%2Fgithubjs&sort=stars&order=desc&page=1&per_page=10 flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════ flutter: ╔ Body flutter: ║ flutter: ║ { flutter: ║ total_count: 1, flutter: ║ incomplete_results: false, flutter: ║ items: [ flutter: ║ { flutter: ║ id: 199987740, flutter: ║ node_id: "MDEwOlJlcG9zaXRvcnkxOTk5ODc3NDA=", flutter: ║ name: "githubjs", flutter: ║ full_name: "upcwangying/githubjs", flutter: ║ private: false, flutter: ║ owner: { flutter: ║ login: "upcwangying", flutter: ║ id: 19725091, flutter: ║ node_id: "MDQ6VXNlcjE5NzI1MDkx", flutter: ║ "https://avatars1.githubusercontent.com/u/19725091?v=4" flutter: ║ gravatar_id: "", flutter: ║ url: "https://api.github.com/users/upcwangying", flutter: ║ html_url: "https://github.com/upcwangying", flutter: ║ "https://api.github.com/users/upcwangying/followers" flutter: ║ "https://api.github.com/users/upcwangying/following{/other_user}" flutter: ║ "https://api.github.com/users/upcwangying/gists{/gist_id}" flutter: ║ "https://api.github.com/users/upcwangying/starred{/owner}{/repo}" flutter: ║ "https://api.github.com/users/upcwangying/subscriptions" flutter: ║ organizations_url: "https://api.github.com/users/upcwangying/orgs", flutter: ║ repos_url: "https://api.github.com/users/upcwangying/repos", flutter: ║ "https://api.github.com/users/upcwangying/events{/privacy}" flutter: ║ "https://api.github.com/users/upcwangying/received_events" flutter: ║ type: "User", flutter: ║ site_admin: false flutter: ║ } flutter: ║ html_url: "https://github.com/upcwangying/githubjs", flutter: ║ "A library to get GitHub trending or Github contributions or Github langua flutter: ║ ges for JavaScript developers." flutter: ║ fork: false, flutter: ║ url: "https://api.github.com/repos/upcwangying/githubjs", flutter: ║ forks_url: "https://api.github.com/repos/upcwangying/githubjs/forks", flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/keys{/key_id}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/collaborators{/collabor flutter: ║ ator}" flutter: ║ teams_url: "https://api.github.com/repos/upcwangying/githubjs/teams", flutter: ║ hooks_url: "https://api.github.com/repos/upcwangying/githubjs/hooks", flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/issues/events{/number}" flutter: ║ events_url: "https://api.github.com/repos/upcwangying/githubjs/events", flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/assignees{/user}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/branches{/branch}" flutter: ║ tags_url: "https://api.github.com/repos/upcwangying/githubjs/tags", flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/git/blobs{/sha}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/git/tags{/sha}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/git/refs{/sha}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/git/trees{/sha}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/statuses/{sha}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/languages" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/stargazers" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/contributors" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/subscribers" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/subscription" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/commits{/sha}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/git/commits{/sha}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/comments{/number}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/issues/comments{/number flutter: ║ }" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/contents/{+path}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/compare/{base}...{head} flutter: ║ " flutter: ║ merges_url: "https://api.github.com/repos/upcwangying/githubjs/merges", flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/{archive_format}{/ref}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/downloads" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/issues{/number}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/pulls{/number}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/milestones{/number}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/notifications{?since,al flutter: ║ l,participating}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/labels{/name}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/releases{/id}" flutter: ║ "https://api.github.com/repos/upcwangying/githubjs/deployments" flutter: ║ created_at: "2019-08-01T06:06:46Z", flutter: ║ updated_at: "2019-10-10T15:19:46Z", flutter: ║ pushed_at: "2019-10-29T17:57:48Z", flutter: ║ git_url: "git://github.com/upcwangying/githubjs.git", flutter: ║ ssh_url: "git@github.com:upcwangying/githubjs.git", flutter: ║ clone_url: "https://github.com/upcwangying/githubjs.git", flutter: ║ svn_url: "https://github.com/upcwangying/githubjs", flutter: ║ homepage: "https://www.npmjs.com/org/githubjs", flutter: ║ size: 438, flutter: ║ stargazers_count: 1, flutter: ║ watchers_count: 1, flutter: ║ language: "JavaScript", flutter: ║ has_issues: true, flutter: ║ has_projects: true, flutter: ║ has_downloads: true, flutter: ║ has_wiki: true, flutter: ║ has_pages: false, flutter: ║ forks_count: 0, flutter: ║ mirror_url: null, flutter: ║ archived: false, flutter: ║ disabled: false, flutter: ║ open_issues_count: 1, flutter: ║ license: { flutter: ║ key: "mit", flutter: ║ name: "MIT License", flutter: ║ spdx_id: "MIT", flutter: ║ url: "https://api.github.com/licenses/mit", flutter: ║ node_id: "MDc6TGljZW5zZTEz" flutter: ║ } flutter: ║ forks: 0, flutter: ║ open_issues: 1, flutter: ║ watchers: 1, flutter: ║ default_branch: "master", flutter: ║ score: 1.0 flutter: ║ } flutter: ║ ] flutter: ║ } flutter: ║ flutter: ╚══════════════════════════════════════════════════════════════════════════════════════════

kimmy-wang commented 4 years ago

@wendux 这个是不是一个bug?

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

lakmalz commented 3 years ago

What happened to these issues? Any answer for this? I have got the same issue Http status error 422

DioError [DioErrorType.response]: Http status error [422]

0 DioMixin.assureDioError (package:dio/src/dio_mixin.dart:819:20

but Same API request is working with Dart http package

BSBre commented 2 years ago

What happened to these issues? Any answer for this? I have got the same issue Http status error 422

DioError [DioErrorType.response]: Http status error [422] #0 DioMixin.assureDioError (package:dio/src/dio_mixin.dart:819:20

but Same API request is working with Dart http package

Any answer here? Got same problem