You have to check the response body, but the issue is probably because you're not sending the whole user model.
hello Anton, thanks for your prompt answers, they are a bit of a novice in programming so I wonder if you mean the
whole user model like all the fields that you mention in the api documentation that is to say this:
"id": 0,
"name": "string",
"email": "string",
"phone": "string",
"readonly": true,
"administrator": true,
"map": "string",
"latitude": 0,
"longitude": 0,
"zoom": 0,
"password": "string",
"twelveHourFormat": true,
"coordinateFormat": "string",
"disabled": true,
"expirationTime": "2019-08-24T14:15:22Z",
"deviceLimit": 0,
"userLimit": 0,
"deviceReadonly": true,
"limitCommands": true,
"fixedEmail": true,
"poiLayer": "string",
"attributes": {}
}```
If the answer is yes, as the only data that I do not want to update is the password, how could I do it?
You get the user first using the API, change the password field and send the whole model back.
Hello, I am experimenting with the traccar API when creating and deleting this, everything is OK, the problem comes when I want to update with the PUT method.
This is an example of my code to update:
$apiUrl = 'https://domain/api/users/'.$userId; $data = array( 'name' => $name, 'password' => $password, 'email' => $email, 'phone' => $phone, 'attributes' => array( 'telegramChatId' => $chatIdTelegram ) ); $jsonData = json_encode($data); $options = array( 'http' => array( 'header' => "Content-type: application/json\r\n" . "Authorization: Bearer $tokenLocation", 'method' => 'PUT', 'content' => $jsonData ) ); $response = file_get_contents($apiUrl, false, stream_context_create($options)); if ($response === false) { echo "Error connecting to Traccar API."; } else { echo $response; }
When I run that code I get this error:
But if I change the method to GET and comment the variable that contains the data, if the Traccar api responds well:
$apiUrl = 'https://domain/api/users/'.$userId; $data = array( 'name' => $name, 'password' => $password, 'email' => $email, 'phone' => $phone, 'attributes' => array( 'telegramChatId' => $chatIdTelegram ) ); $jsonData = json_encode($data); $options = array( 'http' => array( 'header' => "Content-type: application/json\r\n" . "Authorization: Bearer $tokenLocation", 'method' => 'GET', // 'content' => $jsonData ) ); $response = file_get_contents($apiUrl, false, stream_context_create($options)); if ($response === false) { echo "Error connecting to Traccar API."; } else { echo $response; }
The response from the second code block is:
Has anyone had this problem ?