How to handle non-ASCII characters in Api-Username header?

AI-generated summary

The user xbd is facing an issue with the Discourse API when using non-ASCII characters (like ö in foö) in the Api-Username header, as it results in an error stating that headers must contain only ASCII characters. URL-encoding the username fails due to authentication issues. zogstrip confirms this is a known issue and points to a fix in progress (https://github.com/discourse/discourse/pull/37177). In the meantime, they recommend using the Api-User-Id header instead of the username, which is the preferred method when the user ID is available.

Hi,

I’m integrating with the Discourse API using RestSharp and running into an issue with usernames containing non-ASCII characters (specifically German umlauts). I need to impersonate a user with username foö (contains ö).

When sending the raw username in the Api-Username header, I get: Request headers must contain only ASCII characters

When URL-encoding the username (Uri.EscapeDataString("foö") → fo%C3%B), authentication fails with: {"errors":["Du bist nicht berechtigt, die angeforderte Ressource anzuzeigen. Der API-Benutzername oder -Schlüssel ist ungültig."],"error_type":"invalid_access"}(access forbidden / user not existing)

I’m using an API key with admin privileges. Endpoint: POST /posts.json to create topics on behalf of users

What’s the correct way to pass usernames with non-ASCII characters in the Api-Username header? Should I:

  1. Use a different encoding method?
  2. Use a user ID instead (if so, what header name)?
  3. Something else entirely?

Thanks for any guidance!

Nice catch @xbd - this will be fixed by

https://github.com/discourse/discourse/pull/37177

But in the meantime…

… you can use the user id (it’s actually preferred over the username if you have it) via the Api-User-Id header

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