API post request without an Accept header returns 406

AI-generated summary

The API POST endpoint for data explorer queries returns a 406 Not Acceptable error when the Accept header is missing. According to RFC 7231, a request without an Accept header should be treated as */*. mbauman suggests that this issue might not be limited to data explorer queries. sam believes this is a Rails issue, not a Discourse one, and has recategorized the topic as a feature request due to the trivial workaround available by including the Accept header.

RFC 7231 suggests that a request with a missing Accept header should be treated as */*, but hitting the API POST endpoint for a data explorer query without said header returns 406 Not Acceptable. I’m not sure if this is specific to data explorer or if it applies to other POSTs, too. All the other GETs I’ve used haven’t had this issue.

It so happens that curl and Python’s requests append Accept: */* to all their requests by default, but not all tools do. You can see this behavior by disabling curl’s default with an empty header, e.g.,

curl -X POST "https://discourse.example.com/admin/plugins/explorer/queries/26/run" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: <your_all_users_api_key>" \
-H "Api-Username: system" \
-H "Accept:"

Not a major thing, but it might be something that other tools hit, too… and it seems like it could be a simple fix, especially since all other endpoints I’ve used work without it.

2 Likes

This is very likely coming from the Rails stack (not Discourse), very likely complicated to work around.

I can see some assertions in actionpack for 406 status.

Going to recategorize this as Contribute > Feature for now, since there is a trivial workaround and appears to be by design in Rails.

1 Like