Api Call Get/Put Python

Albertus7 years ago

Good Day

Hope Anyone can help me with Rest Api Calls:

I can Get Data:

import json
import sys
import requests 


url = 'http://domain.co.za'
user = 'admin'
password = 'P@ssword'

payload = {'all': 'true', 'id': 5}
response = requests.get(url + ':8082/api/devices', auth=(user, password), params=payload, timeout=1.000)
data = json.loads(response.content)[0]  

,BUT Struggle to PUT data to traccar with Rest api:

import requests
import json

url = "http://domain.co.za:8082/api/devices/"
user = 'admin'
password = 'P@ssword'

data = {'id': 5, 'disabled': 'false'}
headers = {"Content-Type": "application/json"}

response = requests.post(url, auth=(user, password), headers=headers, data=data)

print(response.text)

Thank You

Albertus Geyser

Anton Tananaev7 years ago

You have to send full model, not just the fields that you want to change.

Albertus7 years ago

Could you explain even if on another language?

Anton Tananaev7 years ago

Your data object should be FULL device, not just two fields.

Albertus7 years ago

Anton

What am i missing?

import requests
import json

url = "http://domain:8082/api/devices/3"

headers = {'Content-type': 'application/json', 'Accept': 'application/json'}


user = 'admin'
password = 'password'

data={
'attributes':{},
'category':'person',
'contact':'',
'disabled':'true',
'geofenceIds':{},
'groupId':3,
'id':3,
'lastUpdate':'2017-12-23T07:33:37+02:00',
'model':'',
'name':'Child',
'phone':'',
'positionId':6582,
'status':'offline',
'uniqueId':6880010284
}

response = requests.put(url, auth=(user, password), headers=headers, data=json.dumps(data))

datax = response.json()

print(datax)

Thank You

Albertus Geyser

sokha phan5 years ago

hello

Cris Amende5 years ago

This code works in Python3
Obviously settings need to adjusted to reflect local situation....

import json
import requests


###Settings####
TCurl = ''			#Server address of traccar system i.e. 'http://demo.traccar.org'
TCportR = ''						#port for traccar system usually ':8082' 
Traccar_user = ''				#User for traccar (user needs to have access to group in where devices are located) 
Traccar_password = ''				#Traccar User password

headers = {'Content-type': 'application/json', 'Accept': 'application/json'}

data={
'id':None,
'name':'RWS 20',
'uniqueId':'SEA-244690995',
'lastUpdate':None,
'positionId':'',
'groupId':15,
'attributes':{},
'phone':'',
'model':'',
'contact':'',
'category':'boat',
'disabled':False,
'status':None
}
response = requests.post(TCurl + TCportR + '/api/devices', auth=(Traccar_user, Traccar_password), headers=headers, data=json.dumps(data))
print (response.content)
print (response.status_code)