Passing params to Data Explorer using API requires enclosing a value

Reference: Run Data Explorer queries with the Discourse API

When passing params to a SQL query using the API, Discourse currently requires that all values be encapsulated using double quotes, including integers, which are not encapsulated by default on some systems (namely PHP’s json_encode). Looking at the JSON RFC, there is no requirement to use double quotes for integers. This requirement also goes against the philosophy of “be tolerant in what you accept…”

How to reproduce?

-F 'params={"user_id":2}'

Server returns 500 Internal Server Error.

In error logs this shows up:

NoMethodError (undefined method `downcase' for 2:Integer)
app/controllers/application_controller.rb:424:in `block in with_resolved_locale'
app/controllers/application_controller.rb:424:in `with_resolved_locale'
lib/middleware/omniauth_bypass_middleware.rb:64:in `call'
lib/content_security_policy/middleware.rb:12:in `call'
lib/middleware/csp_script_nonce_injector.rb:12:in `call'
config/initializers/008-rack-cors.rb:14:in `call'
lib/freedom_patches/rails_rack_logger_from_rails_7_2.rb:35:in `call_app'
lib/freedom_patches/rails_rack_logger_from_rails_7_2.rb:22:in `call'
config/initializers/100-quiet_logger.rb:20:in `call'
config/initializers/100-silence_logger.rb:29:in `call'
lib/middleware/missing_avatars.rb:22:in `call'
lib/middleware/turbo_dev.rb:31:in `call'
lib/middleware/processing_request.rb:12:in `call'

Backtrace

plugins/discourse-data-explorer/lib/discourse_data_explorer/parameter.rb:121:in `cast_to_ruby'
...

This format works correctly:

-F 'params={"user_id":"2"}'

I’m sharing this not because I’m nitpicky, but to save time for anyone who may encounter a similar issue as I did. It took me a while to figure out what was causing the problem.

3 Likes

you are a life saver. I just wasted 3+ hours of my life to tackle this.

Do you know how I can handle this in json payload if I do POST call with application/json content to the run endpoint?

Or I have to go with curl?

You haven’t shared any details of the platform you are using so generally:

That depends on the library you’re using to make the POST HTTP request. If the library doesn’t let you wrap the integer in double quotes, you might need to either find a workaround by manually constructing the POST request at a lower level or switch to a different library that supports this functionality.

1 Like

really appreciate your reply and sorry for not providing enough info.

Im using the python json and request packages and was not able to figure out enforcing the double quote

So i eventually used your curl cmd and called via python subprocess.

So many thanks again!

1 Like