# 检查分类名称是否可用。API 预演 / 自定义 slug？

**URL:** https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373
**Category:** Feature
**Created:** [2018年六月21日 07:46 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373 "2018-06-21T07:46:59Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [2018年六月21日 07:46 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/1 "2018-06-21T07:46:59Z")

</div>

I’m facing an issue where I need to check wether creating a category with a given name is possible with the API — in other words, if a category slug is not already taken.

Right now, I check it by fetching `/c/foobar.json` and interpreting it (404 means available).

> User wants to create `Foobar` (slug `foobar`) → checking if `/c/foobar.json` already exists

But if the user wants to create `Foo Bar` (slug `foobar` too) it will raise an error because the slug already exists from another category (`Foobar`).

I’d need either:

- To **call an endpoint made for that** (e.g. `/category-available/name.json`) but I think it’s too specific.
- To **specify the slug as a parameter** of the category creation endpoint.
- To **dry-run the API category creation process**. This could be extended to other creation endpoints.

What do you guys think?

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2018年六月22日 01:07 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/2 "2018-06-22T01:07:39Z")

</div>

If a category name is taken, sending a POST request to `/categories.json` returns an error message:

```plaintext
{"errors":["Category Name has already been taken"]}

```

You could check for this error message.

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [2018年六月22日 08:26 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/3 "2018-06-22T08:26:48Z")

</div>

In the creation process of my application, creating a Discourse category isn’t the only step. Therefore, to avoid being in a inconsistent state, I prefered not to try to create a category to check wether the name is available or not.

To be specific, I also create a GitLab project derived from the same name the user is giving. What if the Discourse category slug is available but not the GitLab slug? The Discourse category would be created but not the GitLab project. Sure I could remove the just-created category in that case, but that’s a lot of spilled electricity 😉

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [2018年六月22日 09:46 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/4 "2018-06-22T09:46:46Z")

</div>

You’ll probably always have to handle problems while creating the category. The name already being taken is not the only possible reason for this to fail, and the category could be created after you checked whether the name is taken 🙂

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [2018年六月22日 10:07 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/5 "2018-06-22T10:07:54Z")

</div>

> [@fefrei](#):
>
> You’ll probably always have to handle problems while creating the category. The name already being taken is not the only possible reason for this to fail

Sure, but I’d need to handle the “name already taken” error specifically in terms of UI/UX. Other errors would not be user-related (because the only thing that ties my user to Discourse at this point is a category name) and would be treated as server errors.

> [@fefrei](#):
>
> and the category could be created after you checked whether the name is taken 🙂

Yes, but checking it by issuing a POST request to `categories.json` would automatically create it if available, not only check it 😕

That’s what I meant in:

> [@ryancey](#):
>
> I prefered not to try to create a category to check wether the name is available or not.

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [2018年六月22日 10:09 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/6 "2018-06-22T10:09:32Z")

</div>

> [@ryancey](#):
>
> Yes, but checking it by issuing a POST request to `categories.json` would automatically create it if available, not only check it 😕

Sorry, that’s not what I meant. Even if you did check whether the name already exists, later on, creating a category might _still_ fail simply because someone else has created the category in the meantime.

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [2018年六月22日 10:31 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/7 "2018-06-22T10:31:59Z")

</div>

Good point. But then I’d treat it like a server error.

My point being: I cannot test wether a category name is already taken or not without having side-effect (trying to create the category) or do it unreliably (trying to fetch the category with the slugged name — slugged with another library than the Discourse’s one so maybe not the exact same slug in some conditions, therefore making the test irrelevant).

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [2018年六月22日 11:45 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/8 "2018-06-22T11:45:02Z")

</div>

Can’t you just check [categories.json](https://meta.discourse.org/categories.json)?

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [2018年六月25日 09:05 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/9 "2018-06-25T09:05:44Z")

</div>

I’d have the same problem with trying to fetch `/c/{slug}.json`. I can’t reliably predict what slug will be produced from a given name, other than running it through the Discourse `Slug` module, which is only possible by creating a category and check the output. But then, side-effect. That’s why I’m looking for a side-effect-free way to check the availability of a category name/slug.

Otherwise, I see that I can in fact choose a slug when creating a category but it’s not documented, and the check wether a category name is available or not is done on the name **and** the slug. Is there a good reason to do so? In my case, most of the categories would be hidden and only accessible from direct link, so it’s not a big deal if multiple categories have the same name (but different slugs).

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [2018年六月25日 09:18 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/10 "2018-06-25T09:18:20Z")

</div>

> [@ryancey](#):
>
> I’d have the same problem with trying to fetch `/c/{slug}.json` .

That’s not what I recommended – can’t you check [`/categories.json`](https://meta.discourse.org/categories.json) instead?

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [2018年六月25日 09:42 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/11 "2018-06-25T09:42:32Z")

</div>

We’ll get there 😃

You recommended to check `/categories.json` for the existence of a category name/slug is that right ?

If so: I can’t check a name and its slug because I can’t predict what slug will be produced from a given name without…

> [@ryancey](#):
>
> … running it through the Discourse `Slug` module, which is only possible by creating a category and check the output.

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [2018年六月25日 09:56 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/12 "2018-06-25T09:56:45Z")

</div>

Possibly stupid question, because I didn’t check, but…  
If you don’t specify a slug, doesn’t Discourse automatically select a free one?

---

<div class="post-metadata">

### Author: ![ryancey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ryancey/32/99309_2.png) [@ryancey](https://meta.discourse.org/u/ryancey)
#### Post date: [2018年六月25日 10:22 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/13 "2018-06-25T10:22:56Z")

</div>

You’re right, but I just saw that in fact Discourse doesn’t allow duplicate category **names** even with different slugs.

I can indeed check if the category name is already present in `/categories.json`, but what about string cleaning, escaping, and such? I can’t be 100% sure the name given by the user in my app front-end will be the same as the one Discourse would use when creating the category.

**Example:** Would `🎹 Category with an emoji` appear as so in `/categories.json` or will the emoji be stripped? If not now, I can’t be sure it won’t be the case tomorrow. Depends on so many factors, from the app to the server.

The only way I can be sure is to feed Discourse this name and get the response. As of today, the only way is to try to create it, and therefore having side-effect.

My feature proposal still stands 🙂 It would be great to have a side-effect free way to check if a category name is available.

Allowing duplicate category names (restricting unicity to slugs) is another request.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [2018年六月27日 05:35 UTC](https://meta.discourse.org/t/check-whether-a-category-name-is-available-api-dry-run-custom-slug/90373/14 "2018-06-27T05:35:16Z")

</div>

Slight correction here - the full list of categories **and subcategories** is actually at `/site.json` .
