Ruby upgrade from version 2.3.2 to 2.7.7 breaks REST API response body

44 views Asked by At

Recently I updated my application's ruby version from 2.3.2 to 2.7.7.

My application uses https://github.com/rest-client/rest-client gem to request data from a 3rd-party API.

While using ruby version 2.3.2 the response body of API request returned expected data as shown below:

url = "<3rd-party API URL>"

headers = { 
  content_type: :json,
  Authorization: "Bearer <API_TOKEN>"
}

RestClient.get(url, headers)

when executed above code on 2.3.2 version irb it returned expected data like following

irb(main):014:0> response.body.encoding
=> #<Encoding:ASCII-8BIT>
irb(main):015:0> response.body
=> "[{\"entity_id\":\"person.admin\",\"state\":\"unknown\",\"attributes\":{\"editable\":true }}]"

But when executed the same code on 2.7.7 irb it returned encoded data like following

irb(main):021:0> response.body
=> "x\x9C\xD5\x9D\xFD\x92\xA2\xC8\x96\xC0_\x85\xA8\x88\x8D\xB87 .... <truncated> ....."
irb(main):022:0> response.body.encoding
=> #<Encoding:ASCII-8BIT>

Based on the encoding information I tried to convert those encoded characters to string but they doesn't match the expected output shown above for 2.3.2 irb. So looks like the actual encoding of the response's body and encoding returned by response.body.encoding actually doesn't match.

Can anybody please let me know what those encoded characters actually represent? And why manually converting them doesn't returned the expected characters? And why such difference in output?

0

There are 0 answers