From 5a62b071f64da212538545061b1eb6d36bd62ee6 Mon Sep 17 00:00:00 2001 From: Artur Date: Wed, 6 Jul 2022 13:19:11 +0200 Subject: [PATCH 1/2] fixed: on status code 301 or 302 redirect --- links/gql_http_link/lib/src/link.dart | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/links/gql_http_link/lib/src/link.dart b/links/gql_http_link/lib/src/link.dart index 74a4fcf9..8fa8f70f 100644 --- a/links/gql_http_link/lib/src/link.dart +++ b/links/gql_http_link/lib/src/link.dart @@ -61,7 +61,7 @@ class HttpLinkResponseContext extends ContextEntry { /// [http.Client] to the constructor. class HttpLink extends Link { /// Endpoint of the GraphQL service - final Uri uri; + Uri uri; /// Default HTTP headers final Map defaultHeaders; @@ -167,6 +167,12 @@ class HttpLink extends Link { final httpRequest = _prepareRequest(request); try { final response = await _httpClient!.send(httpRequest); + if (response.statusCode == 301 || response.statusCode == 302) { + if (response.headers["location"] != null) { + uri = Uri.parse(response.headers["location"]!); + } + return _executeRequest(request); + } return http.Response.fromStream(response); } catch (e) { throw ServerException( @@ -220,10 +226,12 @@ class HttpLink extends Link { return http.MultipartRequest("POST", uri) ..body = httpBody ..addAllFiles(fileMap) + ..followRedirects = true ..headers.addAll(headers); } return http.Request("POST", uri) ..body = httpBody + ..followRedirects = true ..headers.addAll(headers); } From 8c2093e9f658af537badb8e37349fa34acf8254b Mon Sep 17 00:00:00 2001 From: Artur Date: Wed, 6 Jul 2022 13:40:59 +0200 Subject: [PATCH 2/2] added comment and removed null check --- links/gql_http_link/lib/src/link.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/links/gql_http_link/lib/src/link.dart b/links/gql_http_link/lib/src/link.dart index 8fa8f70f..83d67519 100644 --- a/links/gql_http_link/lib/src/link.dart +++ b/links/gql_http_link/lib/src/link.dart @@ -168,9 +168,9 @@ class HttpLink extends Link { try { final response = await _httpClient!.send(httpRequest); if (response.statusCode == 301 || response.statusCode == 302) { - if (response.headers["location"] != null) { - uri = Uri.parse(response.headers["location"]!); - } + // `location` header will never be null on statusCode 301 or 302. + // More info. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302 + uri = Uri.parse(response.headers["location"]!); return _executeRequest(request); } return http.Response.fromStream(response);