Error in request api/users method PUT

Manuel Suárez 3 years ago

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:

Warning: file_get_contents(https://app.location.mx/api/users/69): Failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

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:

{id: 69, attributes: {pushoverUserKey: "1", telegramChatId: "5", ui.disableAttributes: true,…},…}
...

Has anyone had this problem ?

Anton Tananaev 3 years ago

You have to check the response body, but the issue is probably because you're not sending the whole user model.

Manuel Suárez 3 years ago

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?
Anton Tananaev 3 years ago

You get the user first using the API, change the password field and send the whole model back.