I am trying to update the toaldistance and hours with a python script
but I got an error 405
Error updating device accumulators: 405 Client Error: Method Not Allowed for url: https://traccar.ipcs.synology.me/devices/4/accumulators
here is my python script :
import requests
import json
def update_device_accumulators(server_url, device_id, total_distance, hours, api_key):
"""
Updates the total distance and hours of a device using the Traccar API.
Args:
server_url (str): The base URL of the Traccar server.
device_id (int): The ID of the device to update.
total_distance (float): The new total distance in meters.
hours (float): The new total hours.
api_key (str): The API key for authentication.
Returns:
bool: True if the update was successful, False otherwise.
"""
url = f"{server_url}/devices/{device_id}/accumulators"
headers = {
"Authorization": f"Basic {api_key}", # Assuming Basic Auth
"Content-Type": "application/json"
}
payload = {
"deviceId": device_id,
"totalDistance": total_distance,
"hours": hours
}
try:
response = requests.put(url, headers=headers, json=payload)
response.raise_for_status() # Raise an exception for bad status codes
return True
except requests.exceptions.RequestException as e:
print(f"Error updating device accumulators: {e}")
return False
except json.JSONDecodeError as e:
print(f"Error decoding response: {e}")
return False
# Example Usage (replace with your actual values)
server_url = "https://traccar.ipcs.synology.me" # Replace with your Traccar server URL
device_id = 4 # Replace with the actual device ID
total_distance = 10000.0 # Replace with the new total distance
hours = 5.0 # Replace with the new total hours
api_key = "Traccar" #replace with your username and password
success = update_device_accumulators(server_url, device_id, total_distance, hours, api_key)
if success:
print("Device accumulators updated successfully!")
else:
print("Failed to update device accumulators.")
I am stuck, thanks for any help
I think you're missing api in the path. In general I recommend comparing your API requests with what the official web app sends.
if I api I got this error :
Error updating device accumulators: 400 Client Error: Bad Request for url: https://traccar.ipcs.synology.me/api/devices/4/accumulators
If I try to reset the totaldistance from the web UI, the url is :
https://traccar.ipcs.synology.me/settings/accumulators/4
what is correct ?
I'm talking about the API web uses, not the web page URL.
ok, but if I add api in the url I got error 400
I am new with traccar, how to see the API web use ?
Typically you would use your browser developer tools. It's not Traccar specific.
ok, but with python, I have no browser developer tools ?
must the api be added in the url ?
https://traccar.ipcs.synology.me/api/devices/4/accumulators
but with api added I got an error 400
and without api I got an error 405
thanks for your help
ok, it works now... the json was not good
I am trying to update the toaldistance and hours with a python script
but I got an error 405
here is my python script :
I am stuck, thanks for any help