I am working on a project where I have to GET data from a link and then POST the data in another server after I have extracted the information that I needed from the data fetched. I am using the library requests for my GET and POST, and here's the code for extracting the data needed:
''' original data fetched
result= {"version": "1.0","cmd":"list_metering","status":"success","devices":
[{"deviceid":"xxxxx","model":"xxxx"},{"deviceid":"xxxxx","model":"xxxx"}]} '''
for devices in result['devices']:
# delete the parameters I don't need
final_data = removekey(devices,'model')
# x.update(final_data) -> Trying dicts
# x.append(final_data) -> Trying lists
# Sending directly
resp = requests.post(url,json=final_data,headers=headers)
no+=1
if no== len(result['devices']):
break
This is where I will call a function that will delete keys that are not needed and then I will take the rest and post it.
and I tried using the update() function for dictionaries but it didn't work due to the fact that I have the same keys so only one of the data will be considered. Lists work but I will get the data in this form:
[{"deviceid":"xxxxx","model":"xxxx"},{"deviceid":"xxxxx","model":"xxxx"}]
and I tried to use json=data and x.json() but both didn't work out for lists. However, if I send the data directly as it is shown in my first code, due to delays and I am not so sure what else, if i am lucky, full data will be sent, otherwise I keep losing some part of the data fetched.
How do I GET data in json and then send it back again in json for this case? My aim is to send the data as one bundle, all the devices together so I don't lose anything.
Here are the GET and POST I am using:
# GET
url_source = 'https://website'
url = requests.get(url_source)
result = url.json()
# POST
headers = {'charset':'utf-8','Content-Type':'application/json'}
url = "http://xxxx/_get_v1.php"
data = final_data
resp = requests.post(url,json=data,headers=headers)