github icon indicating copy to clipboard operation
github copied to clipboard

unexpected token when calling repos.contents.readme with "html" media type

Open Naatan opened this issue 10 years ago • 2 comments

Calling the API like this:

github.repos.contents.readme user, repo, accept: 'application/vnd.github.v3.html'

Fails with error:

/home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/json-1.8.2/lib/json/common.rb:155:in `parse': 795: unexpected token at '<div class="announce instapaper_body md" data-path="readme.md" id="readme"><article class="markdown-body entry-content" itemprop="mainContentOfPage"><h1> (MultiJson::ParseError)

I removed the rest of the error as its just the parsed readme in HTML.

Stack trace:

    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/json-1.8.2/lib/json/common.rb:155:in `parse'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/multi_json-1.10.1/lib/multi_json/adapters/json_common.rb:16:in `load'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/multi_json-1.10.1/lib/multi_json/adapter.rb:20:in `load'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/multi_json-1.10.1/lib/multi_json.rb:119:in `load'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/jsonable.rb:12:in `decode'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/response/jsonize.rb:13:in `block in <class:Jsonize>'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/response/jsonize.rb:25:in `call'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/response/jsonize.rb:25:in `parse'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/faraday-0.9.1/lib/faraday/response.rb:16:in `on_complete'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/faraday-0.9.1/lib/faraday/response.rb:9:in `block in call'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/faraday-0.9.1/lib/faraday/response.rb:57:in `on_complete'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/faraday-0.9.1/lib/faraday/response.rb:8:in `call'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/faraday-0.9.1/lib/faraday/response.rb:8:in `call'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/request/basic_auth.rb:16:in `call'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/faraday-0.9.1/lib/faraday/request/url_encoded.rb:15:in `call'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/faraday-0.9.1/lib/faraday/request/multipart.rb:14:in `call'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/request/jsonize.rb:23:in `call'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/faraday-0.9.1/lib/faraday/rack_builder.rb:139:in `build_response'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/faraday-0.9.1/lib/faraday/connection.rb:377:in `run_request'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/faraday-0.9.1/lib/faraday/connection.rb:140:in `get'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/request.rb:68:in `call'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/request/verbs.rb:20:in `get_request'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/client/repos/contents.rb:30:in `readme'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/api.rb:224:in `block in execute'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/api.rb:211:in `run_callbacks'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/api.rb:223:in `execute'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/github_api-0.12.3/lib/github_api/api.rb:183:in `block in method_added'
    from /home/nathan/Projects/foo/lib/foo/command.rb:120:in `block in parse_category'
    from /home/nathan/Projects/foo/lib/foo/command.rb:66:in `each'
    from /home/nathan/Projects/foo/lib/foo/command.rb:66:in `parse_category'
    from /home/nathan/Projects/foo/lib/foo/command.rb:42:in `block in resources'
    from /home/nathan/Projects/foo/lib/foo/command.rb:41:in `each'
    from /home/nathan/Projects/foo/lib/foo/command.rb:41:in `resources'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/middleman-core-3.3.10/lib/middleman-core/cli.rb:72:in `method_missing'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/thor-0.19.1/lib/thor/command.rb:29:in `run'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/thor-0.19.1/lib/thor/command.rb:126:in `run'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/middleman-core-3.3.10/lib/middleman-core/cli.rb:20:in `start'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/gems/middleman-core-3.3.10/bin/middleman:18:in `<top (required)>'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/bin/middleman:23:in `load'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/bin/middleman:23:in `<main>'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/bin/ruby_executable_hooks:15:in `eval'
    from /home/nathan/.rvm/gems/ruby-1.9.3-p547/bin/ruby_executable_hooks:15:in `<main>'

Naatan avatar Mar 15 '15 21:03 Naatan

I am experiencing the same issue when attempting to pass accept: 'application/vnd.github.v3.raw' or accept: 'application/vnd.github.v3.html'. As best as I can tell it appears that, no matter the media/response type, the gem passes the content through the Response::Jsonize.define_parser method, and therefore through JSON.parse. Is this the intentional behavior and, if so, is there an appropriate way to retrieve non-JSON content (raw, html, etc) through just the baseline GitHub API gem functionality? If not, I assume overriding is the next best solution?

Thanks for any assistance!

GabeStah avatar Mar 30 '17 12:03 GabeStah

@GabeStah I ran into same issue just now and found that you can supply the raw: true parameter and it will skip adding the Jsonize Faraday middleware:

https://github.com/piotrmurach/github/blob/7381b431af71ae553bdbb604221127d2274fa53b/lib/github_api/middleware.rb#L24

E.g.:

client.pull_requests.get("blah", "blah", 1, accept: 'application/vnd.github.diff', raw: true)

nickmerwin avatar Jul 08 '17 22:07 nickmerwin