Discourse Data Explorer SQL Query

AI-generated summary

In a Discourse Data Explorer SQL Query support thread, JohnMcDonald reports an issue running a parameterized query via API, receiving a “Missing parameter topic_id” error despite passing it in params. Arkshine suggests sending the parameters in the data payload as a JSON string instead of URL params. After experimenting, JohnMcDonald confirms success using data={'params': '{"topic_id": "398"}'}, noting the need to escape quotes on Windows and clarifying that params in Python’s requests module is for URL query strings, not POST body data. Arkshine adds that the same approach works on both Linux (WSL) and Windows without escaping issues, sharing a working example and screenshot.

I have a question that is very similar to (but different from) the question posted here: `DataExplorer::ValidationError: Missing parameter` when running Data Explorer queries with [params] via API.

My query in Data Explorer looks like this:

-- [params]
-- topic_id :topic_id

SELECT * FROM posts WHERE topic_id = :topic_id

This works fine in Data Explorer. Then, when I try and run it via the API I get:

{"success":false,"errors":["DiscourseDataExplorer::ValidationError: Missing parameter topic_id of type topic_id"]}'

Here is my python request:


headers = {
    'Content-Type': 'multipart/form-data;',
    'Api-Key': API_KEY,
    'Api-Username': USERNAME_SYSTEM
}

params = {"topic_id": 398}

response = requests.post(url="[REDACTED]/admin/plugins/explorer/queries/10/run", headers=headers, params=params)

I gather from the error than I cannot pass “topic_id” as a string, but I don’t see how else I can pass it as a key. Any thoughts on this?

The parameters are not passed in the URL.

Maybe something like: data={“params”: ‘{“topic_id”:"398"}’?

I had to try a lot of different alternatives, but this worked:

                         headers=headers, data={'params': '{\"topic_id\": "398"}'})```

So, the answer is a combination of that prior post (which said you have to escape the quotes when you are sending from a Windows machine and Arkshine's post which says it needs to be passed as data and not as a params kwarg.  What I find odd though, is I thought the whole point of the params kwarg in the Python requests module was to pass parameter information.

I’m glad it worked!

I tried sending Linux (wsl) or Windows; I don’t have the same issue, and it worked for both. :thinking:

data = {
    'params': '{"topic_id": "3"}'
}

response = requests.post(
    url="http://localhost:4200/admin/plugins/explorer/queries/4/run", 
    headers=headers, 
    data=data
)

image