add new device

Hans Sachs7 years ago

it's possible to add a new device with a command as shell command ?

Anton Tananaev7 years ago

Yes, it should be possible. You can use curl to send corresponding API call.

Hans Sachs7 years ago

I have search a long time and test many things, but I cannot add an device via curl!

I can get a list of my devices with:

curl -u USER:PASSWORD http://1.2.3.4:8082/api/devices

I test in serveral variations:

curl -u ADMIN:PASSWORD  --data '{"name":"test1","uniqueId":"test1"}' http://1.2.3.4:8082/api/devices/add
curl -u ADMIN:PASSWORD  --data '{"name":"test1","uniqueId":"test1"}' http://1.2.3.4:8082/tracar/rest/addDevice

please give me an working example

Anton Tananaev7 years ago

The URL you should be using is

http://1.2.3.4:8082/api/devices

You need to send a POST request.

Hans Sachs7 years ago

Traccar Ver 3.16::

I also used (https://www.traccar.org/api-reference/)

curl -X POST -u ADMIN:PASSWORD  --data '{"name":"test1","uniqueId":"test1"}' http://1.2.3.4:8082/api/devices

or

curl -X POST -u ADMIN:PASSWORD  --data "{\"name\":\"test1\",\"uniqueId\":\"test1\"}" http://1.2.3.4:8082/api/devices

or

curl -X POST -u ADMIN:PASSWORD  --data "name=test1&uniqueId=test1" http://1.2.3.4:8082/api/devices

response:

HTTP 415 Unsupported Media Type - NotSupportedException (...)
Anton Tananaev7 years ago

You need to pass content type for JSON.

Hans Sachs7 years ago

ah ...

curl -X POST -H "Content-Type: application/json" -u ADMIN:PASSWORD  --data '{"name":"test1","uniqueId":"test1"}' http://1.2.3.4:8082/api/devices

indeed - it works

:) nice day :)

Nitish Khadaria6 years ago

i am using same command (curl -X POST -H "Content-Type: application/json" -u admin:admin --data '{"name":"test1","uniqueId":"test1"}' http://127.0.0.1:8082/api/devices) but device not add in database and return (Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 2]). So how to fixed it and important point is i have change database to oracle and successfully created table in database.

mob-devices4 years ago

Unexpected character is caused on Windows, you must put double quotes, like example is below

curl -X POST -H `Content-Type: application/json" -u user:password  --data "{"""name""":"""my-device-name""","""uniqueId""":"""my-device-unique-id"""}" http://1.2.3.4:8082/api/devices
Natan4 years ago

I tried to use this command, but i received this error:

NullPointerException (BaseObjectResource:120 < ...)