# Unicode usernames

**URL:** https://meta.discourse.org/t/unicode-usernames/277878
**Category:** Development
**Tags:** rest-api
**Created:** [September 5, 2023, 4:50pm UTC](https://meta.discourse.org/t/unicode-usernames/277878 "2023-09-05T16:50:44Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![zengyunsi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zengyunsi/32/265722_2.png) [@zengyunsi](https://meta.discourse.org/u/zengyunsi)
#### Post date: [September 5, 2023, 4:50pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/1 "2023-09-05T16:50:44Z")

</div>

Currently discourse unicode usernames does not work with the api

Is it possible to fix this bug?

 ![1693932591022](https://global.discourse-cdn.com/meta/original/4X/3/2/0/320efd5a14d792cdc4ef9869431f4c32eeed5e00.png)

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [September 5, 2023, 5:06pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/2 "2023-09-05T17:06:41Z")

</div>

> [@zengyunsi](#):
>
> Currently discourse unicode usernames does not work with the api

I just tried this and it works for me:

```plaintext
○ → curl -s -L -H 'api-key: «redacted»' -H 'api-username: doesnotexist' https://try.discourse.org/u/运思/notifications.json
{"errors":["You are not permitted to view the requested resource. The API username or key is invalid."],"error_type":"invalid_access"}

○ → curl -s -L -H 'api-key: «redacted»' -H 'api-username: 运思' https://try.discourse.org/u/运思/notifications.json | jq '.users[0]'
{
  "id": 41,
  "username": "运思",
  "name": "Michael Brown",
  "avatar_template": "/user_avatar/try.discourse.org/运思/{size}/208_2.png",
  "trust_level": 1,
  "assign_icon": "user-plus",
  "assign_path": "/u/运思/activity/assigned"
}

```

Can you demonstrate how it fails for you?

---

<div class="post-metadata">

### Author: ![zengyunsi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zengyunsi/32/265722_2.png) [@zengyunsi](https://meta.discourse.org/u/zengyunsi)
#### Post date: [September 5, 2023, 7:02pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/3 "2023-09-05T19:02:22Z")

</div>

> [@supermathie](#):
>
> `api-key:`

**Thank you, Michael Brown.**

I have tested it and if I test it in the browser it is ok, but in the programming software it is not ok, because the rule is that only ascii can be used to transfer this protocol.

Reason for the error:

TypeError: Invalid character in header content [“Api-Username”]

Please refer to the log, it is recommended to add ut8 encoding or decoding once for matching

1. **Test interface**

> **[Discourse API Docs](https://docs.discourse.org/#tag/Posts/operation/listPosts)**

 ![image](https://global.discourse-cdn.com/meta/original/4X/7/7/7/7774fc1102ec210997225039b8b20f2ae2c36a46.png)

2. **web address**

[https://xxxxxxxx/posts.json](https://xxxxxxxx/posts.json)

1. 

```plaintext
Accept:application/json
api-key:f9c13aafa9b21baf778161bff66a62533ba650b6e4542e3e9788e98c55ded869
Api-Username:system

```

**Test results** :

OK

```plaintext
Accept:application/json
api-key:f9c13aafa9b21baf778161bff66a62533ba650b6e4542e3e9788e98c55ded869
Api-Username:风之旅人

```

{“status”:500,“error”:“Internal Server Error”}

```plaintext
Accept:application/json
api-key:f9c13aafa9b21baf778161bff66a62533ba650b6e4542e3e9788e98c55ded869
Api-Username:%E8%BF%90%E6%80%9D

```

You are not permitted to view the requested resource. The API username or key is invalid

**Reason for error** :

The use of Chinese in the header is invalid, it needs to be encoded by encodeURI() in order to be transmitted to the server, but the server needs to decode decodeURI() in order to recognize it.

Suggest to add a judgment, if not found, try to decodeURI, match the username.

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [September 5, 2023, 7:17pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/4 "2023-09-05T19:17:07Z")

</div>

> [@zengyunsi](#):
>
> in the programming software it is not ok

What are you using? The error looks like python? This is supported by `requests` but possibly not by the libraries you’re using.

```python
In [1]: import requests

In [2]: api_key = '«redacted»'

In [3]: r = requests.get('https://try.discourse.org/u/运思/notifications.json', headers = {
  'api-key': api_key,
  'api-username': '运思'.encode()
})

In [4]: r.status_code
Out[4]: 200

In [5]: r.json()['users'][0]
Out[5]: 
{'id': 41,
 'username': '运思',
 'name': 'Michael Brown',
 'avatar_template': '/user_avatar/try.discourse.org/运思/{size}/208_2.png',
 'trust_level': 1,
 'assign_icon': 'user-plus',
 'assign_path': '/u/运思/activity/assigned'}

```

Try calling `.encode()` to explicitly convert the header value to `bytes`… though admittedly since not everything is 8-bit clean I do agree this suggestion:

> [@zengyunsi](#):
>
> add a judgment, if not found, try to decodeURI, match the username

is reasonable.

---

<div class="post-metadata">

### Author: ![zengyunsi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zengyunsi/32/265722_2.png) [@zengyunsi](https://meta.discourse.org/u/zengyunsi)
#### Post date: [September 6, 2023, 9:42am UTC](https://meta.discourse.org/t/unicode-usernames/277878/6 "2023-09-06T09:42:34Z")

</div>

Search the forums Older versions of the api can be used with Query

Newer versions of the api don’t work Query

Is it compatible with Query Fix this issue

例如：

```plaintext
https://www.xxx.com/posts.json?api_key=714552c6148e1617aeab526d0606184b94a80ec048fc09894ff1a72b740c5f19&api_username=system

```

Hopefully, this will be resolved.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [September 6, 2023, 1:49pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/7 "2023-09-06T13:49:37Z")

</div>

> [@zengyunsi](#):
>
> Hopefully, this will be resolved.

It won’t, you now need to pass the API parameters as headers instead of query parameters. That won’t change.

---

<div class="post-metadata">

### Author: ![zengyunsi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zengyunsi/32/265722_2.png) [@zengyunsi](https://meta.discourse.org/u/zengyunsi)
#### Post date: [September 6, 2023, 1:55pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/9 "2023-09-06T13:55:14Z")

</div>

But the header can not pass non-ASCII characters, I want to pass the Chinese user name

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [September 6, 2023, 1:56pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/10 "2023-09-06T13:56:45Z")

</div>

> [@zengyunsi](#):
>
> But the header can not pass non-ASCII characters

It can, as proven by @supermathie above:

cURL

> [@supermathie](#):
>
> ```plaintext
> ○ → curl -s -L -H 'api-key: «redacted»' -H 'api-username: 运思' https://try.discourse.org/u/运思/notifications.json | jq '.users[0]'
> {
> "id": 41,
> "username": "运思",
> "name": "Michael Brown",
> "avatar_template": "/user_avatar/try.discourse.org/运思/{size}/208_2.png",
> "trust_level": 1,
> "assign_icon": "user-plus",
> "assign_path": "/u/运思/activity/assigned"
> }
> 
> ```

Python:

> [@supermathie](#):
>
> ```plaintext
> In [1]: import requests
> 
> In [2]: api_key = '«redacted»'
> 
> In [3]: r = requests.get('https://try.discourse.org/u/运思/notifications.json', headers = {
> 'api-key': api_key,
> 'api-username': '运思'.encode()
> })
> 
> In [4]: r.status_code
> Out[4]: 200
> 
> In [5]: r.json()['users'][0]
> Out[5]: 
> {'id': 41,
> 'username': '运思',
> 'name': 'Michael Brown',
> 'avatar_template': '/user_avatar/try.discourse.org/运思/{size}/208_2.png',
> 'trust_level': 1,
> 'assign_icon': 'user-plus',
> 'assign_path': '/u/运思/activity/assigned'}
> 
> ```

---

<div class="post-metadata">

### Author: ![zengyunsi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zengyunsi/32/265722_2.png) [@zengyunsi](https://meta.discourse.org/u/zengyunsi)
#### Post date: [September 6, 2023, 2:01pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/12 "2023-09-06T14:01:43Z")

</div>

In fact, it does not work, I have tried, because the header can not contain non-ASCII characters, you need to do an encoding conversion, but after encoding the characters, it will prompt the user name does not exist, because in the discourse on the receiving end did not restore!!

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [September 6, 2023, 2:20pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/13 "2023-09-06T14:20:29Z")

</div>

> [@zengyunsi](#):
>
> because in the discourse on the receiving end did not restore

The headers need to be encoded as UTF-8, not with URI encoding.

---

<div class="post-metadata">

### Author: ![zengyunsi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zengyunsi/32/265722_2.png) [@zengyunsi](https://meta.discourse.org/u/zengyunsi)
#### Post date: [September 6, 2023, 4:33pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/15 "2023-09-06T16:33:19Z")

</div>

1. 1: the header cannot contain Chinese characters
2. 2: utf8 refers to the content in the post, not the header
3. 3: Discourse is not decoded by UrlEncode, it will show that the username does not exist.

**The following test data is provided**

```plaintext
https://www.baowei.ink/posts.json

```

```plaintext
{
  "title": "标题标题标题标题标题",
  "raw": "标题标题标题标题标题标题标题标题标题标题",
  "category": 10,
}

```

**Pass**

```plaintext
Content-Type: application/json; charset=UTF-8
Api-Key: «redacted»
Api-Username: system

```

**Fail**

```plaintext
Content-Type: application/json; charset=UTF-8
Api-Key: «redacted»
Api-Username: 风之旅人

```

```plaintext
Content-Type: application/json; charset=UTF-8
Api-Key: «redacted»
Api-Username: %E9%A3%8E%E4%B9%8B%E6%97%85%E4%BA%BA

```

Thank you! Michael Brown

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [September 6, 2023, 4:37pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/16 "2023-09-06T16:37:30Z")

</div>

You’ll want to rotate that API key **immediately** @zengyunsi, consider it leaked.

… though I can confirm it does work when you encode the headers with UTF-8:

```python
In [1]: import requests

In [2]: api_key = '«redacted»'

In [3]: r = requests.get('https://www.baowei.ink/posts.json', headers={'api-key': api_key, 'api-username': '风之旅人'.encode()})

In [4]: r.status_code
Out[4]: 200

In [5]: len(r.json()['latest_posts'])
Out[5]: 19

```

---

<div class="post-metadata">

### Author: ![zengyunsi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zengyunsi/32/265722_2.png) [@zengyunsi](https://meta.discourse.org/u/zengyunsi)
#### Post date: [September 7, 2023, 5:04pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/17 "2023-09-07T17:04:14Z")

</div>

Is this the case and can you provide an example, thanks!

```plaintext
Content-Type: Application/json; charset=utf-8
Accept: Application/json; charset=utf-8
Api-Key: f9c13aafa9b21baf778161bff66a62533ba650b6e4542e3e9788e98c55ded869
Api-Username: \xe9\xa3\x8e\xe4\xb9\x8b\xe6\x97\x85\xe4\xba\xba

```

Always prompt  
You are not permitted to view the requested resource. The API username or key is invalid

---

<div class="post-metadata">

### Author: ![hawm](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@hawm](https://meta.discourse.org/u/hawm)
#### Post date: [September 10, 2023, 3:14pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/18 "2023-09-10T15:14:37Z")

</div>

I can confirmed that below example would work:

```plaintext
$ curl -s -L \
-H 'api-key: 60834ccb8eda28cf17bab8efbbb2fbf874eb8b5a75ff17ddbd2346eb292881a0' \
-H 'api-username: 测试用户' \
http://localhost:4200/u/%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7/emails.json

{"email":"discobot_email","secondary_emails":[],"unconfirmed_emails":[],"associated_accounts":[]}

```

And without `api-*` header:

```plaintext
$ curl -s -L \
http://localhost:4200/u/%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7/emails.json

{"errors":["You need to be logged in to do that."],"error_type":"not_logged_in"}

```

Python requests example:

```plaintext
import requests

api_key = '60834ccb8eda28cf17bab8efbbb2fbf874eb8b5a75ff17ddbd2346eb292881a0'
api_username = '测试用户'
api_endpoint = 'http://localhost:4200/u/%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7/emails.json'

r = requests.get(api_endpoint, headers={
                 'api-key': api_key, 'api-username': api_username.encode()})

print(r)

<Response [200]>

```

> [@zengyunsi](#):
>
> ```plaintext
> Api-Username: \xe9\xa3\x8e\xe4\xb9\x8b\xe6\x97\x85\xe4\xba\xba
> 
> ```

Seems your `api-username` does not encode appropriately

```plaintext
>>> '风之旅人'.encode()
b'\xe9\xa3\x8e\xe4\xb9\x8b\xe6\x97\x85\xe4\xba\xba'

```

> <https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal>

---

<div class="post-metadata">

### Author: ![zengyunsi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zengyunsi/32/265722_2.png) [@zengyunsi](https://meta.discourse.org/u/zengyunsi)
#### Post date: [September 10, 2023, 3:18pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/19 "2023-09-10T15:18:18Z")

</div>

Thank you all, I’m going to try, if you can use the Chinese user name to call the api operation is more convenient, thank you!

---

<div class="post-metadata">

### Author: ![Lhc\_fl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lhc_fl/32/268115_2.png) [@Lhc\_fl](https://meta.discourse.org/u/Lhc_fl)
#### Post date: [September 10, 2023, 3:18pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/20 "2023-09-10T15:18:37Z")

</div>

![image](https://global.discourse-cdn.com/meta/original/4X/0/3/8/038741375083f66f1fe0f8a904a7a734c6e34a82.jpeg)  
This is a bit weird and I can’t use the api normally either

---

<div class="post-metadata">

### Author: ![hawm](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@hawm](https://meta.discourse.org/u/hawm)
#### Post date: [September 10, 2023, 3:25pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/21 "2023-09-10T15:25:41Z")

</div>

It much likely an issue with the API key scope.

---

<div class="post-metadata">

### Author: ![zengyunsi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zengyunsi/32/265722_2.png) [@zengyunsi](https://meta.discourse.org/u/zengyunsi)
#### Post date: [September 10, 2023, 3:25pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/22 "2023-09-10T15:25:48Z")

</div>

😅 I tried it and it doesn’t seem to work either, but I don’t know Python.  
The same prompt appeared…

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [September 10, 2023, 11:52pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/23 "2023-09-10T23:52:32Z")

</div>

Does the same call with an ASCII username work?

(I’m guessing no and that’s what you mean by “I can’t use the API normally”)

If so, does the website have another proxy in front of it or is it using the [standard install](https://meta.discourse.org/t/142537?silent=true)?

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [September 10, 2023, 11:55pm UTC](https://meta.discourse.org/t/unicode-usernames/277878/24 "2023-09-10T23:55:41Z")

</div>

> [@hawm](#):
>
> Seems your `api-username` does not encode appropriately

Yeah, [this call](https://meta.discourse.org/t/unicode-usernames/277878/17) encoded it as a escaped representation of the UTF-8 byte sequence, rather than the bytes themselves.

[Next page](https://meta.discourse.org/t/unicode-usernames/277878.md?page=2)
