How to post a new position from an api with osmand protocol

35TlEcDva year ago

Hello,

I tried to post a new position with a new value in traccar from my api using osmand protocol but it return me error 405 and i don't know what i'm doing wrong.

const api_url = "http://srv/api/positions/";

const api_url_test = "http://srv:5055/?id=17362&lat={13.6666}&lon={80.23000}&timestamp={2016-01-27%2018:55:47}&hdop={3}&altitude={33}&speed={23}"

// Set the headers
const headers = {
  "Content-Type": "application/json",
  "Accept": "application/json",
  "Authorization": "Basic " + Buffer.from("username:password").toString('base64')
};

// Set the payload data
const payload_data = {
  "deviceId": 17362,
  "time": "2023-05-15T10:00:00.000Z",
  "latitude": 37.1234,
  "longitude": -122.5678
};

// Convert the payload data to a JSON string
const payload_json = JSON.stringify(payload_data);

// fetch(api_url_test)
// Send the POST request with the payload data
fetch(api_url, {
  method: 'POST',
  headers: headers,
  body: payload_json
})
.then(response => {
  // Check if the request was successful
  if (response.status === 200) {
    console.log("Position added successfully!");
  } else {
    console.log("Failed to add position. Error code:", response.status);
  }
})
.catch(error => {
  console.error('Error:', error);
});

Can someone tell me what i'm doing wrong and if im on a good way to post a new position value for my device n°17362 ?

Thank you.

Anton Tananaeva year ago

Your code is not using OsmAnd. It seems like you're trying to post to an API endpoint that doesn't exist.

35TlEcDva year ago

Oh ok.. Thank you for answering me, do you have an example of how I could make a post with the osmand protocol via my api?

Anton Tananaeva year ago

Yeah, check the Traccar Client code.

35TlEcDva year ago

Thank you i will look at it !

Anna year ago

Hi All,
I searched on the forum, but did not find such a case. I have a problem with the OsmAnd protocol. My broker sends a message:

10:31:27.334	info	javascript.0 (69200) script.js.62a7_-_OK: curl http://11.10.12.16:5055/?id=62a7&lat=49.08796&lon=22.56570×tamp=1684225914&altitude=68&speed=0

However, there is an answer in the server logs:

GET /?id=62a7 HTTP/1.1
Host: 11.10.12.16:5055
User-Agent: curl/7.81.0
Accept: /

The above messages come to me every 5 minutes (this is how the device is set).

And below you have logs that jumped me correctly only once today ...

2023-05-16 09:58:42  INFO: [T774c2e5e] connected
2023-05-16 09:58:42  INFO: [T774c2e5e: osmand < 11.10.12.16] 474554202f3f69643d3632613720485454502f312e310a486f73743a2031312e31302e31322e31363a353035350a557365722d4167656e743a206375726c2f372e38312e300a4163636570743a202f
2023-05-16 09:58:42  INFO: [T774c2e5e: osmand > 11.10.12.16] 485454502f312e3120323030204f4b0d0a636f6e74656e742d6c656e6774683a20300d0a0d0a
2023-05-16 09:58:43  INFO: [T774c2e5e] id: 62a7, time: 2023-05-15 10:21:21, lat: 49.08796, lon: 22.56570, course: 0.0
2023-05-16 09:58:43  INFO: [T774c2e5e] disconnected

It's some kind of black magic...
Will you help me understand and fix it? Please...

Anton Tananaeva year ago

Clearly some issue with your broker, so I'm not sure how we can help.

Anna year ago

Anton,
Why do you think with a broker? I sent you the data sent from the broker above. It looks like properly configured osmand code from the traccar website (https://www.traccar.org/osmand/)

Anton Tananaeva year ago

Because it doesn't send the data?

Anna year ago

Anton,

I don't know if you noticed in the first message, but I send using CURL, so the broker has nothing to do here. When I type a CURL with OsmAnd code on the command line, I get the correct answer. Also, a string of data, consistent with your example of the OsmAnd protocol, goes to Traccar. You have everything in my first post. Step by step - from sending CURL to Traccar logs. Strange.

One more thing, I used the second device - the same script. The device looks like this in the logs:

2023-04-28 10:42:38  INFO: [T4ee1951e] id: 60p8, time: 1970-01-01 01:00:00, lat: 0.00000, lon: 0.00000, course: 0.0

Although the broker sent the correct data...

Anna year ago

Dear Sirs, Dear Anton,
I noticed that the problem still arises... I have done many tests of the broker and all the data comes out of it correctly. Below I send what I get from the broker in the Traccar logs:

POST /?id=62a7&lat=49.08796&lon=22.56570×tamp=1687772082&altitude=0&speed=0&batt=58&hdop=9 HTTP/1.1
Accept: application/json, text/plain, */*
Content-Type: application/x-www-form-urlencoded
User-Agent: axios/0.27.2
Host: 11.10.12.16:5055
Connection: close
Content-Length: 0

LOGS from Traccar:

2023-06-26 11:37:45  INFO: [T5a484487] connected
2023-06-26 11:37:45  INFO: [T5a484487: osmand < 11.10.12.16] 504f5354202f3f69643d36326137266c61743d34392e3038373936266c6f6e3d32322e35363537302674696d657374616d703d3136383737373230383226616c7469747564653d302673706565643d3026626174743d35382668646f703d3920485454502f312e310a4163636570743a206170706c69636174696f6e2f6a736f6e2c20746578742f706c61696e2c202a2f2a0a436f6e74656e742d547970653a206170706c69636174696f6e2f782d7777772d666f726d2d75726c656e636f6465640a557365722d4167656e743a206178696f732f302e32372e320a486f73743a2031312e31302e31322e31363a353035350a436f6e6e656374696f6e3a20636c6f73650a436f6e74656e742d4c656e6774683a2030
2023-06-26 11:37:45  INFO: [T5a484487: osmand > 11.10.12.16] 485454502f312e3120323030204f4b0d0a636f6e74656e742d6c656e6774683a20300d0a0d0a
2023-06-26 11:37:45  INFO: [T5a484487] disconnected

The problem is rather in Traccar, but I can not find where....

I ask you for help. I have no idea anymore.

Please.... Don't ignore my question, be gentlemen, Anna.

Thank You.

Anton Tananaeva year ago

Are you sure you're using the latest version of Traccar?

Anna year ago

I am using version 5.4 and I did an update to 5.8 -

I noticed an error:

MessageBodyReader not found for media type=text/plain;charset=utf-8, type=interface javax.json.JsonObject, genericType=interface javax.json.JsonObject. - MessageBodyProviderNotFoundException (... < NotificatorTraccar:106 < NotificationManager:110 < ... < *:107 < ... < *:106 < *:148 < ConnectionManager:247 < ...)

and in my broker server -
I noticed an error: socket hang up

Anton Tananaeva year ago

Seems like an issue with push notifications.

Anna year ago

I agree. Do you have any idea why? Any suggestion?