Need Help updating profile info via API

I’ve found a few examples in these forums of updating user info via the API but can’t get it to work. I’m a junior developer so maybe I’m missing something obvious.

First I tried getting this example to work:
curl -X PUT -d 'name=new name’ -L 'https://mysite.com/users/user12345.json?api_key=mykey&api_username=username'

but I get [“BAD CSRF”] so I tried this:

curl -X PUT \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: mykey" \
  -H "Api-Username: username" \
  -d '{"name”: “new name"}' \
  -L 'https://mysite.com/users/user12345.json'

this returns “success”:“OK”, followed by all the json for the user but doesnt actually do any updates.

Am i missing something?

On a related note: are you able to update the username using the same approach?

Thanks for your help.

Yes, this way has been deprecated in favor of passing the credentials in the header (as below).

You mixed quote characters: " and . This might be the reason here. :thinking:

4 Likes

Thanks @Arkshine . Have made sure all the quote characters are the same " but its still not working.

Can’t figure out what the issue is.

Have you tried Reverse engineer the Discourse API to check what happens when you use the UI to perform the change?

It works for me when sending a PUT request to eg. /u/test_one.json

1 Like

Thanks @JammyDodger . I did as you suggested and updated the name in the admin UI and then looked in the network tab. As you mentioned it does the update at a different url (/u instead of /users), but when I change the URL it’s still not working.

Under “Form Data”, I’m looking at the request and it is: name: "newname" just like I’m sending it.

As far as I can tell everything looks right and its still not updating. Stumped.

Ok… I spent a lot more time digging through the forums and found a similar issue from 7 years ago. Turns out if you update the Content-Type to “application/json; charset=utf-8” it works!

I have one last issue: how do I update the username via the API?

I’ve tried doing something like this but it doesnt work:

-d '{"name”: “new name", "username": "new username"}' \

1 Like

Ok… I was finally able to figure this out. Will leave a little summary here in case someone in the future is looking for how to do this. The other answers I found on the forums were out of date.

To update the name via API:
curl -X PUT -H "Content-Type: application/json; charset=utf-8" -H "Api-Key: mykey" -H "Api-Username: username" -d '{"name": “mynewname"}' -L 'https://mysite.com/u/user12345.json'

To update the username via API:
curl -X PUT -H "Content-Type: application/json; charset=utf-8" -H "Api-Key: myapikey" -H "Api-Username: username" -d '{"new_username": "mynewusername"}' -L 'https://mysite.com/u/user12345/preferences/username.json'

4 Likes

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