# Need Help updating profile info via API

**URL:** https://meta.discourse.org/t/need-help-updating-profile-info-via-api/306686
**Category:** Development
**Tags:** rest-api
**Created:** [May 3, 2024, 9:03pm UTC](https://meta.discourse.org/t/need-help-updating-profile-info-via-api/306686 "2024-05-03T21:03:55Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Earnie\_Baird](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/earnie_baird/32/380079_2.png) [@Earnie\_Baird](https://meta.discourse.org/u/Earnie_Baird)
#### Post date: [May 3, 2024, 9:03pm UTC](https://meta.discourse.org/t/need-help-updating-profile-info-via-api/306686/1 "2024-05-03T21:03:55Z")

</div>

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:

```plaintext
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.

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [May 3, 2024, 10:14pm UTC](https://meta.discourse.org/t/need-help-updating-profile-info-via-api/306686/2 "2024-05-03T22:14:01Z")

</div>

> [@Earnie\_Baird](#):
>
> but I get [“BAD CSRF”] so I tried this:

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

> [@Earnie\_Baird](#):
>
> `-d '{"name”: “new name"}' \`

You mixed quote characters: `"` and `”`. This might be the reason here. 🤔

---

<div class="post-metadata">

### Author: ![Earnie\_Baird](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/earnie_baird/32/380079_2.png) [@Earnie\_Baird](https://meta.discourse.org/u/Earnie_Baird)
#### Post date: [May 4, 2024, 6:57am UTC](https://meta.discourse.org/t/need-help-updating-profile-info-via-api/306686/3 "2024-05-04T06:57:33Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [May 4, 2024, 7:53am UTC](https://meta.discourse.org/t/need-help-updating-profile-info-via-api/306686/4 "2024-05-04T07:53:01Z")

</div>

Have you tried [Reverse engineer the Discourse API](https://meta.discourse.org/t/reverse-engineer-the-discourse-api/20576) 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`

---

<div class="post-metadata">

### Author: ![Earnie\_Baird](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/earnie_baird/32/380079_2.png) [@Earnie\_Baird](https://meta.discourse.org/u/Earnie_Baird)
#### Post date: [May 4, 2024, 8:21am UTC](https://meta.discourse.org/t/need-help-updating-profile-info-via-api/306686/5 "2024-05-04T08:21:36Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Earnie\_Baird](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/earnie_baird/32/380079_2.png) [@Earnie\_Baird](https://meta.discourse.org/u/Earnie_Baird)
#### Post date: [May 4, 2024, 12:30pm UTC](https://meta.discourse.org/t/need-help-updating-profile-info-via-api/306686/6 "2024-05-04T12:30:42Z")

</div>

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"}' \`

---

<div class="post-metadata">

### Author: ![Earnie\_Baird](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/earnie_baird/32/380079_2.png) [@Earnie\_Baird](https://meta.discourse.org/u/Earnie_Baird)
#### Post date: [May 4, 2024, 7:49pm UTC](https://meta.discourse.org/t/need-help-updating-profile-info-via-api/306686/7 "2024-05-04T19:49:03Z")

</div>

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'`

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [June 3, 2024, 7:49pm UTC](https://meta.discourse.org/t/need-help-updating-profile-info-via-api/306686/8 "2024-06-03T19:49:05Z")

</div>

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