Discourse Data Explorer SQL 查询

我有一个问题,与此处发布的问题非常相似(但有所不同):https://meta.discourse.org/t/dataexplorer-missing-parameter-when-running-data-explorer-queries-with-params-via-api/223812。

我在 Data Explorer 中的查询如下所示:

-- [params]
-- topic_id :topic_id

SELECT * FROM posts WHERE topic_id = :topic_id

这在 Data Explorer 中运行正常。然后,当我尝试通过 API 运行它时,我会收到:

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

这是我的 Python 请求:


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)

我从错误中推断出,我不能将“topic_id”作为字符串传递,但我不知道如何以其他方式将其作为键传递。有什么想法吗?

1 个赞

参数未在 URL 中传递。

也许是这样的:data={“params”: ‘{“topic_id”:\"398\"}’}

我尝试了很多不同的方法,但最终这个奏效了:

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

所以,答案是结合了之前的帖子(其中提到从 Windows 机器发送时必须转义引号)和 Arkshine 的帖子(其中提到它需要作为 data 而不是 params 参数传递。不过,我奇怪的是,我以为 Python requests 模块中的 params 参数的全部意义就是传递参数信息。)
1 个赞

太好了,它奏效了!

我尝试发送 Linux (wsl) 或 Windows;我没有遇到同样的问题,而且两者都奏效了。:thinking:

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

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

image

1 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.