# Api-Usernameヘッダーの非ASCII文字の扱い方

**URL:** https://meta.discourse.org/t/how-to-handle-non-ascii-characters-in-api-username-header/393646
**Category:** Development
**Created:** [2026 年 1 月 16 日午前 6:59 UTC](https://meta.discourse.org/t/how-to-handle-non-ascii-characters-in-api-username-header/393646 "2026-01-16T06:59:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![xbd](https://avatars.discourse-cdn.com/v4/letter/x/b5e925/32.png) [@xbd](https://meta.discourse.org/u/xbd)
#### Post date: [2026 年 1 月 16 日午前 6:59 UTC](https://meta.discourse.org/t/how-to-handle-non-ascii-characters-in-api-username-header/393646/1 "2026-01-16T06:59:54Z")

</div>

こんにちは。

RestSharp を使用して Discourse API と統合していますが、ユーザー名に非 ASCII 文字（特にドイツ語のウムラウト）が含まれている場合に問題が発生しています。ユーザー名 `foö`（`ö` を含む）をエミュレートする必要があります。

`Api-Username` ヘッダーに生のユーザー名を使用すると、次のエラーが表示されます。`Request headers must contain only ASCII characters`（リクエストヘッダーには ASCII 文字のみを含める必要があります）

ユーザー名を URL エンコードする (`Uri.EscapeDataString("foö")` → `fo%C3%B6`) と、認証が失敗し、次のエラーが表示されます。`{"errors":["Du bist nicht berechtigt, die angeforderte Ressource anzuzeigen. Der API-Benutzername oder -Schlüssel ist ungültig."],"error_type":"invalid_access"}`（アクセス拒否 / ユーザーが存在しない）

管理者権限を持つ API キーを使用しています。エンドポイントは、ユーザーに代わってトピックを作成するための `POST /posts.json` です。

非 ASCII 文字を含むユーザー名を `Api-Username` ヘッダーで渡す正しい方法はありますか？次のいずれかを行うべきでしょうか。

1. 別のエンコード方法を使用する？
2. ユーザー ID を使用する（その場合、ヘッダー名は？）。
3. 全く別の方法？

ご指導いただけると幸いです！

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2026 年 1 月 16 日午後 3:03 UTC](https://meta.discourse.org/t/how-to-handle-non-ascii-characters-in-api-username-header/393646/3 "2026-01-16T15:03:52Z")

</div>

@xbd さん、良い指摘ありがとうございます。これは以下で修正されます。

> <https://github.com/discourse/discourse/pull/37177>
>
> Refactors username lookups throughout the codebase to consistently use \`User.nor…malize\_username\`, \`User.find\_by\_username\`, or the new \`User.where\_username\` scope.
> 
> Previously, username lookups were inconsistent - some used \`.downcase\`, others used \`normalize\_username\`. This consolidation ensures all lookups go through the same normalization logic.
> 
> Changes:
> 
> \- Adds \`User.where\_username\` scope for multi-user lookups
> \- Replaces manual \`.downcase\` lookups with \`find\_by\_username\` or \`where\_username\`
> \- Updates ILIKE/LIKE queries to use \`username\_lower\` with normalized input
> \- Fixes a pre-existing SQL injection vulnerability in search user ordering
> \- Updates bulk import scripts and migrations for consistency
> 
> Ref - https://meta.discourse.org/t/393646

しかし、それまでの間…

> [@xbd](#):
>
> - ユーザーIDを使用する（その場合、ヘッダー名は？)

…`Api-User-Id` ヘッダーを介してユーザーID（実際には、持っている場合はユーザー名よりも優先されます）を使用できます。

```bash
curl -X GET "https://your-discourse.com/admin/users.json" \
  -H "Api-Key: your-api-key" \
  -H "Api-User-Id: 123"

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2026 年 2 月 15 日午後 3:04 UTC](https://meta.discourse.org/t/how-to-handle-non-ascii-characters-in-api-username-header/393646/4 "2026-02-15T15:04:30Z")

</div>

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