How to decode a Gzip Http Response in Flutter?
Asked Answered
S

3

9

I am new to Flutter. I am making a Network Request and I am getting the correct response, but the data is Gzipped. I have decompressed the same in Swift, but with Flutter, I am unable to do that. Can anyone please help?

Here's what I have tried:

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');
    }
  }

The expected response is JSON, but as it is Gzipped and I am unable to decompress it, I do not know how to get the JSON from it. I have tried decoding it and it gives me gzipped_data as a List of Integers as you can see above. How can I get the JSON from it?

Spanjian answered 17/8, 2019 at 10:24 Comment(0)
M
5

I'm using GZipCodec from dart:io.

Try it like this to return the response as a 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);

  }
Marcoux answered 6/2, 2020 at 8:48 Comment(1)
This sounds like the correct answer, and yet, I get "FormatException (FormatException: Filter error, bad data)"Uplift
F
3

The body of a response will be automatically uncompressed.

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

Fordo answered 2/7, 2020 at 17:5 Comment(1)
Also, the HttpClient will automatically add the Accept-Encoding: gzip-header. See: api.flutter.dev/flutter/dart-io/HttpClient-class.html#headersBehre
S
0

This worked for me! And I'm guessing that Dio automatically handles the decompression when the response is received in gzip format!

@lazySingleton
Dio dio(@Named('BaseUrl') String url) {
  final gZipCodec = GZipCodec();
  final dio = Dio(
    BaseOptions(
      baseUrl: url,
      contentType: Headers.jsonContentType,
      connectTimeout: const Duration(seconds: 120),
      receiveTimeout: const Duration(seconds: 120),
      sendTimeout: const Duration(seconds: 120),
    ),
  );

  dio.interceptors.add(InterceptorsWrapper(
    onRequest: (options, handler) {
      final jsonData = jsonEncode(options.data);
      final compressedData = gZipCodec.encode(utf8.encode(jsonData));
      options.data = compressedData;
      options.headers['Content-Encoding'] = 'gzip';
      options.headers['Accept-Encoding'] = 'gzip';
      options.headers['Content-Type'] = 'application/json';
      return handler.next(options);
    },
    onResponse: (response, handler) {
      return handler.next(response);
    },
  ));

  return dio;
}
Sankhya answered 23/8, 2024 at 9:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.