Problem adding users to groups from the API

Samuel4 years ago

Hello, we are developing a web application that makes use of Traccar. Through cURL, we create, edit and delete users. So far so good.

We have the problem when adding the user to a group. We do it through the following cURL instruction:

curl -X POST -H "Content-Type: application/json" -H "Authorization: Basic admin:adminpass" -d "{'userId': 72, 'groupId': 3}" "https://server.com/api/permissions" --cacert "cacert-2019-01-23.pem" > request.txt

When executing the previous instruction, the user is not added to the group and the request.txt file shows the following content:

ArrayIndexOutOfBoundsException

I've checked the Traccar API documentation (Link Here) but I am not able to solve the problem. I keep the userId parameter first and groupId second. What am I doing wrong?

Greetings and thank you.

Anton Tananaev4 years ago

Your JSON looks invalid. Why are you using single quotes?

Samuel4 years ago

Hi Anton.

Thanks for answering. I use the single quotes because in the cURL statement where I add the post (-d) parameters I use the double quotes.

I have tried reversing the order, using single quotes in the cURL statement and double quotes in the JSON and it also throws an error. In this case it says it can't find host 72, 3, etc.

I have also tried to have a JSON file and from cURL load its content using -d @file.json, but the result is the same:

ArrayIndexOutOfBoundsException.

What should be the correct structure of the JSON file? When creating users I have not had any problem.

Regards, and thank you very much.

Anton Tananaev4 years ago

I would recommend comparing your API request with what the official web app uses. Just check browser developer tools.

Samuel4 years ago

Actually, we are using the API through PHP using curl(). The procedure to build the JSON is always the same:

$data = array (
   "userId" => $obj->id,
   "groupId" => $obj->group_id
);

Then we use json_encode () and make the call to cURL.

curl_setopt_array ($curl, array (
   CURLOPT_URL => $request_url,
   CURLOPT_USERPWD => $userpwd,
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_ENCODING => "",
   CURLOPT_MAXREDIRS => 10,
   CURLOPT_TIMEOUT => 0,
   CURLOPT_FOLLOWLOCATION => true,
   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
   CURLOPT_CUSTOMREQUEST => $method,
   CURLOPT_POSTFIELDS => json_encode($data),
   CURLOPT_HTTPHEADER => array (
      "Content-Type: application/json"
   ),
));

We use the same procedure to add users, devices, etc. We only have problems adding users to groups.

Anton Tananaev4 years ago

Wait wait wait... you can't add users to groups. I guess I misread your original request. It doesn't make sense. You can add groups to users, but not the other way around.

Samuel4 years ago

Ok, but the procedure is the same, right?

That is, a JSON whose first parameter is userID and the second parameter is groupID, as I can read in the API documentation here.

I guess it's a misconception of mine, but I don't know how I can bind a group to a user via the API.

Anton Tananaev4 years ago

Yes, correct. So, you are trying to add a group to a user. Then it should work. Then I'm back to the original suggestion - compare your request with the official web app API request.

Samuel4 years ago

Okay, Anton.

When I get back to the office I will try. Thanks a lot.