Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#498] Handle utf-8 bom for json parsing #520

Merged
merged 1 commit into from
Apr 21, 2017

Conversation

rsullivan00
Copy link
Contributor

@rsullivan00 rsullivan00 commented Apr 17, 2017

Closes #498

Currently, HTTParty errors when you try to parse JSON that has a utf-8 BOM. Since .NET applications automatically prepend the BOM characters, this severely limits the usefulness of HTTParty when talking to Windows-based JSON services.

This PR resolves that issue by stripping the BOM characters before parsing the JSON response.

def json
JSON.parse(body, :quirks_mode => true, :allow_nan => true)
JSON.parse(body.gsub(/^#{UTF8_BOM}/, ''), :quirks_mode => true, :allow_nan => true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the right spot for this logic, or if I should be checking encoding somehow before trying to strip the BOM.

I'm open to suggestions, if there is a better way to do this.

@jnunemaker
Copy link
Owner

I really don't know much about this, but it seems like an ok fix to me. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BOM characters \xEF\xBB\xBF
2 participants