lineofflight / peddler

Amazon Selling Partner API (SP-API) in Ruby
https://lineofflight.github.io/peddler/
MIT License
307 stars 130 forks source link

MWS get_report encoding issues not UTF-8 but UTF8 #144

Closed NaitoYuki closed 4 years ago

NaitoYuki commented 4 years ago

Hi, Thanks for the this library. We are using MWS::Reports::Client.

We are using get_report(request_id) method for _GET_MERCHANT_LISTINGS_ALL_DATA_ report.

some response is the below error. unknown encoding name - UTF8

28640:E, [2020-03-25T19:50:14.690306 #5105] ERROR -- : unknown encoding name - UTF8
28642-E, [2020-03-25T19:50:14.691057 #5105] ERROR -- : /var/www/app/shared/bundle/ruby/2.6.0/gems/peddler-2.1.1/lib/peddler/headers.rb:50:in `find'
28643-/var/www/app/shared/bundle/ruby/2.6.0/gems/peddler-2.1.1/lib/peddler/headers.rb:50:in `content_charset'
28644-/var/www/app/shared/bundle/ruby/2.6.0/gems/peddler-2.1.1/lib/peddler/flat_file_parser.rb:42:in `scrub_body!'
28645-/var/www/app/shared/bundle/ruby/2.6.0/gems/peddler-2.1.1/lib/peddler/flat_file_parser.rb:21:in `initialize'
28646-/var/www/app/shared/bundle/ruby/2.6.0/gems/peddler-2.1.1/lib/peddler/parser.rb:21:in `new'
28647-/var/www/app/shared/bundle/ruby/2.6.0/gems/peddler-2.1.1/lib/peddler/parser.rb:21:in `new'
28648-/var/www/app/shared/bundle/ruby/2.6.0/gems/peddler-2.1.1/lib/peddler/client.rb:118:in `run'
28649-/var/www/app/shared/bundle/ruby/2.6.0/gems/peddler-2.1.1/lib/mws/reports/client.rb:165:in `get_report'

I found the error is the below. match_data is UTF8. https://github.com/hakanensari/peddler/blob/1e1dde3e597d0ecf1be812468d5001fceb00f59e/lib/peddler/headers.rb#L47

Seems to me something is the wrong encoding pattern.

So, We are avoiding this error for this patch code.

config/initializers/monkey_patches/flat_file_parser.rb

module Peddler
  class FlatFileParser < SimpleDelegator
    def scrub_body!(encoding)
      return if body.encoding == Encoding::UTF_8

      set_charset = content_charset rescue Encoding::UTF_8.to_s
      self.body = body.dup.force_encoding(set_charset || encoding)
    end
  end
end

We hope this error will be resolved.

hakanensari commented 4 years ago

@NaitoYuki Thanks for reporting, I pushed a fix to master

NaitoYuki commented 4 years ago

@hakanensari Super Thanks!!!