# API: cannot update user

**URL:** https://meta.discourse.org/t/api-cannot-update-user/63876
**Category:** Development
**Tags:** rest-api
**Created:** [June 3, 2017, 9:35am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876 "2017-06-03T09:35:06Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Massimo\_Redaelli](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/massimo_redaelli/32/120795_2.png) [@Massimo\_Redaelli](https://meta.discourse.org/u/Massimo_Redaelli)
#### Post date: [June 3, 2017, 9:35am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/1 "2017-06-03T09:35:06Z")

</div>

I’m trying to use the API to sync user data.

I was able to, for instance, add a user to a group with a `PUT` to `groups/:id/members.json`

However, now I’d like to update fields in the user profile – the name, for instance. I think it’s possible, based on [this example](https://github.com/discourse/discourse_api/blob/master/examples/update_user.rb).

So I’m posting request:

```
PUT /users/m.redaelli HTTP/1.1
HOST: disc.uaar.it
accept: application/json
content-length: 107
content-type: application/x-www-form-urlencoded

api_key=xxxx&api_username=m.redaelli&name=Pippo

```

using the master key.

As a result I get a 404 with an HTML page response saying that the requested page does not exist or is private.

Can you help? 🙂

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [June 3, 2017, 10:02am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/2 "2017-06-03T10:02:24Z")

</div>

Hi @Massimo_Redaelli,

Here is a working curl example with an `application/json` Content-Type header and a json body:

```plaintext
curl -X "PUT" "http://localhost:4000/users/joffreyjaffeux?api_key=b07032eebba01d9f200771a28efc0874014501bc9b866460206626172691be0d&api_username=joffreyjaffeux" \
     -H "Content-Type: application/json" \
     -d $'{
  "name": "newname"
}'

```

---

<div class="post-metadata">

### Author: ![Massimo\_Redaelli](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/massimo_redaelli/32/120795_2.png) [@Massimo\_Redaelli](https://meta.discourse.org/u/Massimo_Redaelli)
#### Post date: [June 3, 2017, 10:11am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/3 "2017-06-03T10:11:21Z")

</div>

Thanks!

I tried just now, but with the same result.  
Do I have to enable some settings somewhere? 😕

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [June 3, 2017, 10:11am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/4 "2017-06-03T10:11:46Z")

</div>

Can you paste the exact query you used please?

---

<div class="post-metadata">

### Author: ![Massimo\_Redaelli](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/massimo_redaelli/32/120795_2.png) [@Massimo\_Redaelli](https://meta.discourse.org/u/Massimo_Redaelli)
#### Post date: [June 3, 2017, 10:16am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/5 "2017-06-03T10:16:42Z")

</div>

Sure:

```bash
curl -X "PUT" "https://disc.uaar.it/users/m.redaelli?api_key=***&api_username=m.redaelli" \
     -H "Content-Type: application/json" \
     -d $'{
  "name": "Pippo"
}'

```

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [June 3, 2017, 10:19am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/6 "2017-06-03T10:19:11Z")

</div>

You are still getting an html page with this ?

---

<div class="post-metadata">

### Author: ![Massimo\_Redaelli](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/massimo_redaelli/32/120795_2.png) [@Massimo\_Redaelli](https://meta.discourse.org/u/Massimo_Redaelli)
#### Post date: [June 3, 2017, 10:19am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/7 "2017-06-03T10:19:47Z")

</div>

Yep.

Might https be a problem?

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [June 3, 2017, 10:25am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/8 "2017-06-03T10:25:20Z")

</div>

I suspect you are stopped before reaching the Discourse Api due to a session being lost between `http://disc.uaar.it/` and `https://www.uaar.it`. Can’t help much, all I can tell you is to dig in this direction and the query I gave you is working.

---

<div class="post-metadata">

### Author: ![Massimo\_Redaelli](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/massimo_redaelli/32/120795_2.png) [@Massimo\_Redaelli](https://meta.discourse.org/u/Massimo_Redaelli)
#### Post date: [June 3, 2017, 10:26am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/9 "2017-06-03T10:26:44Z")

</div>

I see.

But then why would it work for setting the usergroups? 😕

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [June 3, 2017, 11:12am UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/10 "2017-06-03T11:12:47Z")

</div>

I don’t know, if i try to do your request I get the following:

```plaintext
HTTP/1.1 403 Forbidden
Server: nginx
Date: Sat, 03 Jun 2017 11:11:27 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Request-Id: 611020b5-db42-4f3f-b38d-a55070e90c59
X-Runtime: 0.018272

{"errors":["Non hai il permesso di vedere la risorsa richiesta."],"error_type":"invalid_access"}

```

Which would prove I’m actually reaching the API. You might want to watch the logs while you are doing the request.

---

<div class="post-metadata">

### Author: ![Massimo\_Redaelli](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/massimo_redaelli/32/120795_2.png) [@Massimo\_Redaelli](https://meta.discourse.org/u/Massimo_Redaelli)
#### Post date: [June 3, 2017, 12:18pm UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/11 "2017-06-03T12:18:01Z")

</div>

That looks better than what I get:

```
HTTP/1.1 404 Not Found
Server: nginx
Date: Sat, 03 Jun 2017 12:14:49 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Discourse-Username: m.redaelli
X-Discourse-Route: users/update
Set-Cookie: _forum_session=T***; path=/; HttpOnly; SameSite=Lax
X-Request-Id: d44c44a2-8d55-451a-a76c-414d3add63ba
X-Runtime: 0.142823

<!DOCTYPE html>
<html lang="it">
...

```

I’m very confused 😕  
But thanks for your help

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [June 3, 2017, 12:20pm UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/12 "2017-06-03T12:20:25Z")

</div>

Your `Content-Type` is wrong:

Yours: Content-Type: text/html; charset=utf-8  
Mine: Content-Type: application/json; charset=utf-8

Try to use this URL:

`https://disc.uaar.it/users/m.redaelli.json?api_key=***&api_username=m.redaelli`

---

<div class="post-metadata">

### Author: ![Massimo\_Redaelli](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/massimo_redaelli/32/120795_2.png) [@Massimo\_Redaelli](https://meta.discourse.org/u/Massimo_Redaelli)
#### Post date: [June 3, 2017, 12:44pm UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/15 "2017-06-03T12:44:39Z")

</div>

Well, that was easier than expected ☀

Actually, one last thing 🙂

How do I update a **custom** field?

Never mind, got it 🙂

{ “user\_fields”: { “the\_id”: “value” } }

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [June 3, 2017, 2:30pm UTC](https://meta.discourse.org/t/api-cannot-update-user/63876/16 "2017-06-03T14:30:01Z")

</div>

Perhaps your API key is wrong?
