FLucas
September 1, 2023, 8:58am
1
Hi,
I’m still importing data from an old app into our new Discourse.
I can’t create a category with its description with the following API call :
curl -X POST "https://site-discourse/categories.json" \
-H "Api-Key: api-key" \
-H "Api-Username: api-username" \
-d "name=categorie test" \
-d "description=Description De La Categorie"
The category is created but there is no description.
I get the following response:
{"category":{"id":21,"name":"categorie test",[...],"slug":"categorie-test", [...] **,"description":null**,"description_text":null,"description_excerpt":null,"topic_url":"/t/a-propos-de-la-categorie-categorie-test/12286", [...]
Thanks for your help
Canapin
(Coin-coin le Canapin)
September 1, 2023, 9:05am
2
Hi
The description of a category is the content of the automatically created “About” topic. The default text is probably considered “null” until it’s edited.
I think there’s no available “description” parameter in the request to create a category, and you need to make another API request to update the first post of the About topic.
Depending on your importing process, you might want to use a proper import script instead of using the API. See migration
1 Like
FLucas
September 1, 2023, 4:49pm
3
Thank you for your reply
I created this script allowing you to create a category and then modify its description:
curl -X POST "https://site-discourse/categories.json" \
-H "Api-Key: api-key" \
-H "Api-Username: api-username" \
-d "name=Catégorie 1"
sleep 2
latest_posts_response=$(curl -X GET "https://site-discourse/posts.json" \
-H "Content-Type: application/json" \
-H "Api-Key: api-key" \
-H "Api-Username: api-username")
post_id=$(echo $latest_posts_response | jq '.latest_posts[0] .id')
curl -X PUT "https://site-discourse/posts/$post_id.json" \
-H "Content-Type: application/json" \
-H "Api-Key: api-key" \
-H "Api-Username: api-username" \
-d '{
"post": {
"raw": "Description de la catégorie 1"
}
}'