[Ruby FAQs] Exception: Zlib::DataError: unknown compression method when use rest-client post data to WeChat Pay Unifiedorder API

Exception: Zlib::DataError: unknown compression method

Recently encountered a problem when we used rest-client, faraday gem or other HTTP client libraries to post data to WeChat Pay Unifiedorder API - https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1.

1
2
3
4
url = "https://api.mch.weixin.qq.com/pay/unifiedorder"
# ...
data = "..."
result = RestClient.post(url, data) # Exception: Zlib::DataError: unknown compression method

Downgrade rest-client version to 2.0.2 in Gemfile, then run bundle update to update Gemfile.lock

1
2
3
4
5
# Gemfile

# rest-client/rest-client: Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.
# https://github.com/rest-client/rest-client
gem 'rest-client', '2.0.2'

Or set Header Accept-Encoding to identity before post data. ruby - Why am I getting Zlib::DataError? - Stack Overflow - https://stackoverflow.com/questions/37926803/why-am-i-getting-zlibdataerror

1
result = RestClient.post(url, data, { 'Accept-Encoding' => 'identity'})

References

[1] rest-client/rest-client: Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions. - https://github.com/rest-client/rest-client

[2] rest-client | RubyGems.org | your community gem host - https://rubygems.org/gems/rest-client

[3] ruby - Why am I getting Zlib::DataError? - Stack Overflow - https://stackoverflow.com/questions/37926803/why-am-i-getting-zlibdataerror

[4] https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 - https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1

[5] Accept-Encoding - HTTP | MDN - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding

[6] lostisland/faraday: Simple, but flexible HTTP client library, with support for multiple backends. - https://github.com/lostisland/faraday