Updating Custom User Field through the API

AI-generated summary

Updating a Custom User Field through the Discourse API can be achieved by sending a PUT request with the correct payload. The payload is not in JSON format, but rather in form data, with the user field value encoded as user_fields%5BID%5D=value (e.g., user_fields%5B8%5D=test).

To update a single user field, use the following format: user_fields%5BID%5D=value, where ID is the ID of the user field to be updated.

However, as Richie discovered, the API call may not work if performed by the user, due to a site setting that prevents editing of user fields by users. To circumvent this, Richie recommends reworking the API call to be performed by the system instead.

I’ve been trying to reverse engineer the updating of a user_field value.

I did this by following the steps in Reverse engineer the Discourse API, I edited a test user and made a note of what was occurring in the network tab of the browser.

I can see that I need to PUT to this url: https://example.com/u/my-username-here.json

Where I got a bit confused was by not seeing a json payload but instead it’s some formdata. The payload was: user_fields%5B1%5D=some+dji%2C+some+Autel&user_fields%5B5%5D=%40UKDroneClub&user_fields%5B6%5D=%40iKiteFlyer&user_fields%5B8%5D=test&timezone=Europe%2FLondon&default_calendar=none_selected

I only wish to update one user_field value,

It’s URL encoded: user_fields%5B8%5D=test

And decoded, it reads as: user_fields[8]=test

(8 = the id of the user_field I want to update)

How would I convert this form data value in to a json payload so I can then PUT it? :thinking:

I’m not sure what I’m doing wrong here but I am able to update the About Me section of a users profile via an API call, but I cannot update any of the user_fields.

If I monitor my browser when editing a user, I can see it PUTing the following:

bio_raw=aboutmetest&user_fields%5B1%5D=some+dji%2C+some+Autel&user_fields%5B5%5D=%40UKDroneClub&user_fields%5B6%5D=%40iKiteFlyer&user_fields%5B8%5D=qwerty&timezone=Europe%2FLondon&default_calendar=none_selected

I am using the following code to make my PUT api call:

' Add some Headers
request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
request.AddHeader("Accept", "application/json")
request.AddHeader("Api-Key", strAPIKey)
request.AddHeader("Api-Username", strUsername)

' Add some form body parameters
request.AddParameter("bio_raw", DateTime.Now())
request.AddParameter("user_fields%5B8%5D", DateTime.Now())
'request.AddParameter("user_fields[8]", DateTime.Now())

Every time I make the API call, sure enough, the bio_raw value gets updated to the current timestamp (as a test).

But for some reason the user_fields[8] value is not updating at all. No errors, it just doesn’t update.

I’ve tried both URL-encoding it as user_fields%5B8%5D and also tried as plain text user_fields[8] but neither have any effect.

Am I missing something obvious here? :thinking:

AARRGGHHH :man_facepalming:

Yes, yes I was.

This:

Prevents the editing of the user_field when the API call is performed by the user.

Of course it does :exploding_head:

If I rework my API call and perform it by system, it works fine :roll_eyes: