在某些 API 调用中,您需要在 POST 数据中传递一个值数组。如何实现这一点?
通过反复试验,我已经弄清楚了如何传递包含单个项目的数组——例如,对于 change-owner,您需要类似以下内容:
username: sean-finnegan
post_ids[]: 925
(或者,在 URL 编码后)
post_ids%5B%5D=925&username=sean-finnegan
在某些 API 调用中,您需要在 POST 数据中传递一个值数组。如何实现这一点?
通过反复试验,我已经弄清楚了如何传递包含单个项目的数组——例如,对于 change-owner,您需要类似以下内容:
username: sean-finnegan
post_ids[]: 925
(或者,在 URL 编码后)
post_ids%5B%5D=925&username=sean-finnegan
好问题,我同意这确实有点令人困惑。如果您想传递多个 post_ids,您可以像下面的 curl 示例一样,为 post_ids[] 传递多个表单数据字段:
curl -i -sS -X POST "http://localhost:8080/t/15/change-owner.json"
-H "Api-Key: api-key" \
-H "Api-Username: system" \
-F "username=blake" \
-F "post_ids[]=29" \
-F "post_ids[]=30"
嘿,Blake,
这仍然是向 API 发送数组的推荐方式吗?我正在尝试创建一个 Webhook,但无法发送 web_hook_event_type_ids 数组参数。
我尝试使用 CURL(作为 multipart/form-data):
curl -X POST "https://MYSITE.com/admin/api/web_hooks" \
-H "Api-Key: KEY" \
-H "Api-Username: USER" \
-F "payload_url=https://MYOTHERSITE.com" \
-F "content_type=1" \
-F "wildcard_web_hook=false" \
-F "verify_certificate=true" \
-F "active=true" \
-F "web_hook_event_type_ids[]=2"
# 我收到
{"errors":["param is missing or the value is empty: web_hook"]}%
还尝试使用 httpie(作为 application/json):
http POST https://MYSITE.com/admin/api/web_hooks \
Api-Key:KEY \
Api-Username:USER \
payload_url=https://MYOTHERSITE.com \
content_type=1 \
wildcard_web_hook:=false \
verify_certificate:=true \
active:=true \
web_hook_event_type_ids:='[2]'
# 我收到
{
"errors": [
"Web hook event types can't be blank"
]
}
啊,非常感谢。Discourse API 文档里可不是这么写的,所以我都快抓狂了。![]()
另外,我注意到即使最终使用了 wildcard_web_hook=true,也必须将 web_hook_event_type_ids 设置为至少包含一个项。这一点也让我困惑了好一会儿。