API: cannot update user


(Massimo Redaelli) #1

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.

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? :slight_smile:


(Joffrey Jaffeux) #2

Hi @Massimo_Redaelli,

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

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

(Massimo Redaelli) #3

Thanks!

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


(Joffrey Jaffeux) #4

Can you paste the exact query you used please?


(Massimo Redaelli) #5

Sure:

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

(Joffrey Jaffeux) #6

You are still getting an html page with this ?


(Massimo Redaelli) #7

Yep.

Might https be a problem?


(Joffrey Jaffeux) #8

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.


(Massimo Redaelli) #9

I see.

But then why would it work for setting the usergroups? :confused:


(Joffrey Jaffeux) #10

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

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.


(Massimo Redaelli) #11

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 :confused:
But thanks for your help


(Joffrey Jaffeux) #12

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


(Massimo Redaelli) #15

Well, that was easier than expected :sunny:

Actually, one last thing :slight_smile:

How do I update a custom field?

Never mind, got it :slight_smile:

{ “user_fields”: { “the_id”: “value” } }


(Jay Pfaffman) #16

Perhaps your API key is wrong?