Working POSTMAN setup for POST to api/devices?

DPB617 years ago

I have a plan to use Traccar with some virtual devices using data from an existing database. I will be using C# to load data from the database to the Traccar API.

Using C# or POSTMAN I have no problem with GET commands to
http://localhost:8082/api/devices
or
http://localhost:8082/api/positions

However, when attempting a POST to api/positions using POSTMAN I am getting various errors, the latest being:
NullPointerException (QueryBuilder:260 < DataManager:205 < DeviceManager:173 < ...)

Is there someone who has managed to get POSTMAN working with the POST command that could show the settings they use as a template for the forum? Reading through the postings I see others having similar problems and having a working POSTMAN sample might help a lot of people out as POSTMAN has a lot of different outputs for different languages.

Anton Tananaev7 years ago

What data are you sending with POST? It has to be full model in JSON format.

DPB617 years ago

I think I am sending all that is required, though not sure whether the formatting is correct. This is what it looks like if I press the CODE button in Postman and select C# format:

var client = new RestClient("http://localhost:8082/api/devices");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "884319bb-9a5e-46e7-aa57-c08cdd451306");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
request.AddHeader("accept", "application/json");
request.AddHeader("authorization", "Basic YWRtaW46YWRtaW4=");
request.AddParameter("application/json", "id=2&name=myname&uniqueId=123&model=12&contact=1234567&phone=1234567&category=1", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Anton Tananaev7 years ago

As far as I can tell from your code, it sends data as form values, not json.

DPB617 years ago

Yeah, that's what I am worried about - which is why I was looking for a sample from POSTMAN so I can see what it should look like. I can't see any examples after a couple of days looking through the forum and can't see any in the API reference.

Hopefully someone else uses POSTMAN and can post a sample here - would help out a lot of other people I see posting similar questions. Or if/when I do figure it out I will post here.

Anton Tananaev7 years ago

I would recommend to look at the requests that official web app sends to the server.

DPB617 years ago

Unfortunately I don't have a place to put the screenshots to link them into here, but I did get Postman working so I could see the code required by my C# application. My settings were:

POST http://localhost:8082/api/devices
Authorization: BasicAuth with admin/admin as username/password
Headers: Authorization - automatically added
         Accept - application/json
         Content-Type - applciation/json
Body: select 'raw' radio button and 'JSON (application/json) from the orange dropdown.
      Then in the text bos just use this, all on one line:
      {"id":4,"name":"Test2","uniqueId":"4","phone":"","model":"","contact":"","category":"","status":"","lastUpdate":null,"groupId":0}

Pressing Send should send to the server and you should see a reply at the bottom indicating acceptance of the new device.
Pressing the orange Code button on the right will allow you to generate code snipped in about 15 different languages plus variants. Hopefully this will help people in the future.