Prevent 'ArgumentError: invalid byte sequence in UTF-8' on Faraday[Ruby]

This is a tips to prevent “ArgumentError: invalid byte sequence in UTF-8“.

Sample Code

1
2
3
4
5
6
7
url = "http://sample.com/search?q=%A0"
Faraday.get(url)
ArgumentError: invalid byte sequence in UTF-8
Faraday.get(url.force_encoding(Encoding::BINARY))
=> #<Faraday::Response:0x007fd8ca826ce8...

Happy Hacking!


Special Thanks