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

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!

1 Like

Nice catch @xbd - this will be fixed by

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"

3 Likes