How do you pass an array to the api

On some of the api calls, you are expected to pass an array of values in the post data. How do you do this?

I have figured out by trial an error how to pass an array with one item in it - e.g. for change-owner, you want something like:

username: sean-finnegan
post_ids[]: 925

(or, in url-encoded)

post_ids%5B%5D=925&username=sean-finnegan

Great question, I agree that can be a bit confusing. If you want to pass multiple post_ids you would pass in multiple form data fields for post_ids[] like this curl example:

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로 보내는 권장 방식인가요? 웹훅을 생성하려고 하는데 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"
    ]
}

이 엔드포인트는 값들이 중첩된 web_hook 파라미터를 요구하는 것 같습니다:

이러한 중첩 형식은 크게 선호하지 않으며, 결국 수정해야 할 목록에 있는 몇 가지 엔드포인트 중 하나입니다. form-data를 사용하여 다음과 같이 API 호출을 수행할 수 있습니다:

각 파라미터가 “array” 구문인 web_hook[payload_url]을 사용하고 있음을 주목해 보세요. JSON으로 요청을 보내는 경우 다음과 같이 보입니다:

{
  "web_hook": {
    "payload_url": "https://example.com/hello",
    "content_type": "1",
    ...
  }
} 

아, 정말 감사합니다. Discourse API 문서에는 그렇게 나와 있지 않아서 정말 당황했어요. :sweat_smile:

또한, wildcard_web_hook=true를 사용하더라도 web_hook_event_type_ids에 최소 하나의 항목을 설정해야 한다는 점도 발견했습니다. 이 부분도 한동안 헤맸던 부분이었어요.