We're Hiring!

Mattermost, Inc.

[SOLVED] How to fetch all the users through curl on apiv4 with Mattermost version 4.1.0

Hi,

Presently I am running MatterMost version 4.1.0 on Ubuntu 16.04. There is a script which automatically runs for disabling the users who leaves my organization. The script is working fine for the apiv3 but not for apiv4. When I try to fetch all the users through the curl command it just fetches only 60 users (by default) whereas in apiv3 I was able to fetch all the users by mentioning the same in the url https://mattermost.example.com/api/v3/users/0/100. How can I achieve the same with the apiv4.

/usr/bin/curl -H “Content-Type: application/json” -H “Authorization: Bearer xxxxxx” -X GET -d ‘{“page”:“0”,“per_page”:“100”}’ https://mattermost.example.com/api/v4/users

Thanks & Regards

Ankush Grover

Those are query parameters not JSON data, so it goes with the URL:

https://mattermost.example.com/api/v4/users?per_page=100

As a curl request it would look something like this:

/usr/bin/curl -H 'Authorization: Bearer xxxxxx' -X GET -d 'per_page=100' -G https://mattermost.example.com/api/v4/users

To use the -d you need to use -G alternatively you could run:

/usr/bin/curl -H 'Authorization: Bearer xxxxxx' -X GET 'https://mattermost.example.com/api/v4/users?per_page=100'

You can skip setting the page since the default is 0 and you want to list all users, so you want page 0 which is the default when not defined.

Thanks Prixone for sharing an example. The below one worked for me but with certain limitation as only 200 users are returned per page.

/usr/bin/curl -H ‘Authorization: Bearer xxxxxx’ -X GET ‘https://mattermost.example.com/api/v4/users?per_page=100

I need another help for deactivating a user, I am trying the below curl command but somehow it is not doing the job and saying “active” parameter is missing in the request body whereas I have supplied the “active” parameter.

/usr/bin/curl -H “Content-Type: application/json” -H “Authorization: Bearer $tokenid” -X PUT -d ‘{“active”: “false”}’ ‘https://mattermost.example.com/api/v4/users/wrjff9bcdid1jd8whm7xs8bgao/active

where wrjff9bcdid1jd8whm7xs8bgao is a user_id of an user.

{id":“api.context.invalid_body_param.app_error”,“message”:“Invalid or missing active in request body”,“detailed_error”:"",“request_id”:“ms15nct1gf8p8bt1kdnfrzweae”,“status_code”:400}

Thanks & Regards

Ankush Grover

Sorry, you’re saying you tried per_page=200 or above and the maximum it ever gave you was 200 no matter how big u changed it to?

Your quotes look odd, at least on my console environment those fail me, maybe that is breaking it?

‘{“active”: “false”}’

versus:

'{"active": false}'

Thanks a lot prixone. Yes its working with -d ‘{“active”: false}’

Related to per_page=200 it always return 200 even I used per_page=1000. So I have use page=$i,per_page=200 with a loop replacing $i with number starting from 0 till 5(I have around 1000 users) to retrieve all my users.

Well I don’t see any limitations on the code for the use of page and per_page, they are translated directly as limit and offset to the database, so it should give you the 1000 if u do per_page=1000

Maybe curl is not reading the whole response or its splitting? Have you checked if the resulting json is valid? from per_page=1000?

Or perhaps its just a bug.

Well I tried on 2 different Ubuntu Systems 14.04 and 16.04 64-bits and the result was same that the curl returned 200 users even though I mentioned ?per_page=1000. Curl is 7.35 version on Ubuntu 14.04 and 7.47 version on Ubuntu 16.04. Do let me know you need any further information.

Curl returned when I used ?per_page=1000

grep -o ‘“id”:’ details.txt | wc -l
200

Curl returned all the users when I mention ?page=1,per_page=200 till page=4,per_page=200

grep -o ‘“id”:’ details.txt | wc -l
859

Thanks & Regards
Ankush Grover

Sorry about that, apparently I missed a small piece of code which is what limits it, thanks @saturnino for pointing that out.

So yes, its not a bug and you would have to page(like you are already doing), if you have more than 200 users as the maximum per_page is 200.

Thanks prixone for the update. Thats fine with me if there are limits in fetching the users.

Thanks once again for your help.

Thanks & Regards

Ankush Grover

No problem, glad it solved you issue :wink:

Is it possible to increase the PER_PAGE_MAXIMUM?

Hi @aholt, you can make multiple requests until you get all the channel members you need

e.g. /api/v4/channels/{channel_id}/members?page=0&per_page=200 , /api/v4/channels/{channel_id}/members?page=1&per_page=200 , and so on until you receive less than 200 results, meaning you’ve hit the end.