Skip to content

Unable to parse response body with bad characters #294

Open
@itskingori

Description

@itskingori

I'm experiencing a JSON::ParserError because of a bad character (in my case it's 0x1b):

Screenshot 2020-03-24 at 16 59 18

Here's the stack trace that showed me where the issue was:

Screenshot 2020-03-24 at 16 55 21

This is the JSON.parse we're hitting ...

when 200, 201, 302
if to_send == "body" && send_json
return JSON.parse(response.body)
elsif to_send == "body"
return response.body
elsif to_send == "code"
return response.code
elsif to_send == "raw"
return response
end

I've been able to replicate using this file (jenkins-log-with-bad-character.txt):

[1] pry(main)> file = File.open("/Users/Kingori/Desktop/jenkins-log-with-bad-character.txt")
=> #<File:/Users/Kingori/Desktop/jenkins-log-with-bad-character.txt>
[2] pry(main)> file_data = file.read
=> "{\"comment\":\"Some gems seem to be missing from your /srv/bundle/cache directory.\\n\eCould not find rake-12.3.3 in any of the sources\"}\n"
[3] pry(main)> JSON.parse(file_data)
JSON::ParserError: 783: unexpected token at '{"comment":"Some gems seem to be missing from your /srv/bundle/cache directory.\nould not find rake-12.3.3 in any of the sources"}
'
from /Users/Kingori/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/json-2.3.0/lib/json/common.rb:156:in `parse'
[4] pry(main)> file_data.encoding
=> #<Encoding:UTF-8>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions