如何在 Flutter 中解码 Gzip Http 响应?

Posted

技术标签:

【中文标题】如何在 Flutter 中解码 Gzip Http 响应?【英文标题】:How to decode a Gzip Http Response in Flutter? 【发布时间】:2019-12-23 09:57:04 【问题描述】:

我是 Flutter 的新手。我正在发出网络请求并且我得到了正确的响应,但数据是 Gzip 压缩的。我已经在 Swift 中解压缩了相同的内容,但是使用 Flutter,我无法做到这一点。有人可以帮忙吗?

这是我尝试过的:

import 'dart:convert';
import 'package:http/http.dart';
import 'package:archive/archive.dart';
import 'package:flutter_app/Constants/constants.dart';

class ServiceHelper 

  static final sharedInstance = ServiceHelper();

  Future<Response> sendRequest(String path, Map<String,dynamic> params) async 

    final String url = Constants.testURL+path;

    var body = jsonEncode(params);

    Response response = await post(url, body:body);

    final gZipped_data = GZipDecoder().decodeBytes(response.bodyBytes);

    print(gZipped_data); //flutter: [123, 34, 115, 116, 97, 116, 117, 115, 34, 58, 50, 48, 48, 44, 34, 109, 101, 115, 115, 97, 103, 101, 34, 58, 34, 83, 105, 103, 110, 32, 105, 110, 32, 67, 111, 109, 112, 108, 101, 116, 101, 100, 32, 83, 117, 99, 99, 101, 115, 115, 102, 117, 108, 108, 121, 34, 44, 34, 100, 97, 116, 97, 34, 58, 123, 34, 117, 115, 101, 114, 95, 105, 110, 102, 111, 34, 58, 123, 34, 112, 108, 97, 121, 101, 114, 95, 105, 100, 34, 58, 49, 52, 56, 54, 57, 44, 34, 112, 108, 95, 97, 112, 112, 95, 105, 100, 34, 58, 34, 57, 90, 104, 57, 82, 66, 108, 70, 34, 44, 34, 112, 114, 111, 102, 105, 108, 101, 95, 112, 105, 99, 34, 58, 34, 104, 116, 116, 112, 115, 58, 92, 47, 92, 47, 119, 119, 119, 46, 115, 112, 111, 114, 116, 115, 113, 119, 105, 122, 122, 46, 99, 111, 109, 92, 47, 118, 51, 92, 47, 105, 109, 103, 92, 47, 112, 108, 97, 121, 101, 114, 115, 92, 47, 112, 105, 99, 92, 47, 105, 80, 45, 49, 53, 50, 51, 56, 54, 57, 50, 57, 56, 46, 112, 110, 103, 34, 44, 34, 101, 109, 97, 105, 108, 34, 58, 34, 116, 101, 99, 104, 121, <…>


   //This throws an error: Unhandled Exception: FormatException: Bad UTF-8 encoding 0x8b (at offset 1)

    return response;

  




//This is the login function and it is in a different file main.dart

 Future<void> login(
      String email,
      String name,
      int source,
      String sourceStr,
      String picString,
      String idToken) async 
    final String playerID = '';

    final String token = await getiosToken().then((token)

      return token;

    );

    Map<String, dynamic> params = 
      "email": email,
      "name": name,
      "player_id": playerID,
      "source_id": source,
      "profile_pic": picString,
      "password": passwordController.text ?? "",
      "ios_token":token,
      "device_type": "I",
      "language_iso": "en",
      "referred_by": "loginDefault",
      "firebase_id_token": idToken,
      "app_version": "4.4.9"
    ;

    Response response = await ServiceHelper.sharedInstance
        .sendRequest(path: Constants.login, params: params);

    if (response.statusCode == 201) 
      // If server returns an OK response, parse the JSON.
      return UserDetails.fromJson(json.decode(response.body));
     else 
      // If that response was not OK, throw an error.
      throw Exception('Failed to load post');
    
  

预期的响应是 JSON,但由于它是 Gzipped 并且我无法解压缩它,我不知道如何从中获取 JSON。我已经尝试对其进行解码,它给了我gzipped_data 作为整数列表,如上所示。如何从中获取 JSON?

【问题讨论】:

【参考方案1】:

我正在使用来自dart:ioGZipCodec

尝试像这样以String 的形式返回响应。

Future<String> sendRequest(String path, Map<String,dynamic> params) async 

    final String url = Constants.testURL+path;

    var body = jsonEncode(params);

    Response response = await post(url, body:body);

    final decoded_data = GZipCodec().decode(response.bodyBytes);


    return utf8.decode(decoded_data, allowMalformed: true);

  

【讨论】:

【参考方案2】:

响应的正文将自动解压缩。

访问https://api.flutter.dev/flutter/dart-io/HttpClient/autoUncompress.html

【讨论】:

另外,HttpClient 会自动添加Accept-Encoding: gzip-header。见:api.flutter.dev/flutter/dart-io/HttpClient-class.html#headers

以上是关于如何在 Flutter 中解码 Gzip Http 响应?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 dart/flutter 中解码 json 数组?

如何在 Flutter 中解码和编码 UTF-8 字符

如何在 dart/flutter 中解码复杂的 json?

本文将向您展示如何在 Flutter 中编码/解码 JSON

StreamReader缓冲GZIP的流式HTTP?

Base64与Gzip编解码插件