Closed Bug 1358328 Opened 8 years ago Closed 4 years ago

API Request is not returning JSON when it should.

Categories

(Webtools Graveyard :: DXR, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED WONTFIX

People

(Reporter: KWierso, Unassigned)

Details

I'm trying to use DXR's JSON API in Treeherder, but I'm having some issues getting it to return JSON. My request sets an Accept header of "application/json, text/plain, */*", which I would expect to prefer application/json, but instead I receive the html search results. If I manually take out the "text/plain, */*", so my Accept header is just "application/json", I do get JSON results, but I'd rather not have to override Angular's $http framework to do that if I don't have to. I'm not entirely sure why it's not sending JSON back for my request. As far as I can see from https://github.com/mozilla/dxr/blob/f3cc71a98975c99c34483fdc827489245b9387b9/dxr/app.py#L690 it should try to take the best option between 'application/json' and 'text/html' from the request's Accept header. Since the only one of those present is 'application/json', I'd assume `best` is 'application/json'. The only thing I can think of is that since `request.accept_mimetypes['text/html']` is not defined, that greater-than check becomes false. I'll put up a totally-untested patch to maybe fix this in a bit.
accept_mimetypes comes from werkzeug: https://github.com/pallets/werkzeug/blob/fbfa21e03286c05aa618138b20ee5823a3bac080/werkzeug/wrappers.py#L1325-L1329 https://github.com/pallets/werkzeug/blob/9c1ef73eb82f37a3b8e6b1f9f62685faae18b2f8/werkzeug/datastructures.py#L1726-L1750 https://github.com/pallets/werkzeug/blob/9c1ef73eb82f37a3b8e6b1f9f62685faae18b2f8/werkzeug/datastructures.py#L1753-L1805 Testing should be possible via: ``` from werkzeug.http import parse_accept_header from werkzeug.datastructures import MIMEAccept accept_mimetypes = parse_accept_header('application/json, text/plain, */*', MIMEAccept) best = accept_mimetypes.best_match(...) ``` At a first glance I'd try making the greater than conditional a greater than or equal to. Alternatively, perhaps this should just be using `accept_json()`? https://github.com/pallets/werkzeug/blob/9c1ef73eb82f37a3b8e6b1f9f62685faae18b2f8/werkzeug/datastructures.py#L1803
Weird, I'm running this script: ``` from werkzeug.http import parse_accept_header from werkzeug.datastructures import MIMEAccept accept_mimetypes = parse_accept_header('application/json, text/plain, */*', MIMEAccept) best = accept_mimetypes.best_match(['application/json', 'text/html']) print accept_mimetypes print best print accept_mimetypes['application/json'] print accept_mimetypes['text/html'] print accept_mimetypes['text/plain'] ``` Which then prints out: ``` text/plain,application/json,*/* application/json 1 1 1 ``` If I change accept_mimetypes like this: ``` accept_mimetypes = parse_accept_header('application/json', MIMEAccept) ``` It prints out: ``` application/json application/json 1 0 0 ``` And if I change it like: ``` accept_mimetypes = parse_accept_header('application/json, text/html', MIMEAccept) ``` It prints out: ``` text/html,application/json application/json 1 1 0 ``` So is it the wildcard at the end that's making this condition useless?
And indeed, if I change the accept_mimetypes line to be only '*/*', each of the checks returns '1'.

DXR is no longer available. Searchfox is now replacing it.
See meta bug 1669906 & https://groups.google.com/g/mozilla.dev.platform/c/jDRjrq3l-CY for more details.

Status: NEW → RESOLVED
Closed: 4 years ago
Resolution: --- → WONTFIX
Product: Webtools → Webtools Graveyard
You need to log in before you can comment on or make changes to this bug.