dset
(d)
June 29, 2021, 8:42am
1
Hi all,
I’m working with request library using post:
if I run:
url = "\"https://server.com/admin/api/api?format=json2&X-API-KEY=internal_batch\""
headers = "\"Content-Type: application/x-www-form-urlencoded\""
data = "\"payload\"="
data += "\'[{\"timestamp\": " + str(1624872100) + ", \"lat\": " + str(0) + ", \"lon\": " + str(0) + "}]\'"
command = "curl -X POST -H " + headers + " -d " + data + " " + url
r = os.system(command)
it works.
but if I run:
API_PROD_URL = "https://server.com/admin"
API_KEY = "X-API-KEY=internal_batch"
API_FORMAT = "format=json2"
url = API_PROD_URL + "/api/api" + "?" + API_FORMAT + "&" + API_KEY
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {"payload":[{"timestamp":1624872100, "lat":0, "lon":0}]}
resp = requests.post(url, headers=headers, data=data)
doesn’t work
I notice that all " are substituted with '. Maybe could be this the problem? can someone help me?
thank you very much in advance
Best regards
When you say it doesn’t work, does it give you an error message? What’s the message?
I also suggest trying it with the json
parameter instead of data
resp = requests.post(url, headers=headers, json=data)
dset
(d)
June 30, 2021, 6:39am
3
thank you for your help.
For example if I run code:
API_PROD_URL = "https://server.com/admin"
API_KEY = "X-API-KEY=internal_batch"
API_FORMAT = "format=json2"
url = API_PROD_URL + "/api/api" + "?" + API_FORMAT + "&" + API_KEY
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {'payload' :[{'timestamp': 1625034450.0, 'lat': 21.236373, 'lon': -76.7993},{'timestamp': 1625034450.0, 'lat': 37.36341, 'lon': -97.543397}]}
resp = requests.post(url, headers=headers, data=data)
or
resp = requests.post(url, headers=headers, json=data)
the resp.text is for “data=data”:
'{"response_output":{"id":666,"stato":0,"messaggio":"no_row_to_write_error","datetime":"2021-06-30T08:33:09+02:00","output":null,"codice_errore":784}}'
or with “json=data”
'{“response_output”:{"id":"missing_params","stato":0,"messaggio":null,"datetime":null,"output":null,"codice_errore":0}}'
thank you very much
best regards