hello! I'm a beginner in Python and i'm doing an internship in an energy company. So the company asked me to write a small python code so that i could retrieve information from different API address to our server.
I first used Postman, and it was very easy to have access to the information:
I told Postman the API address: http://www.adress.com, the API key, and made a GET request to retrieve some information.
Then I used a POST request and had to write a JSON Script in the "Body" section of Postman to have my informations. The following script for example:
{
"structures_name" : [“Ma structure”],
"boitiers_uid" : [“12:34:56:78:90”],
"dates" : ["20160606 06:06","20160707 06:06"],
"IO_name" : ["Mon entrée","Ma sortie"],
"IO_type": ["TEMPERATURE"],
"period_length": ["year","month"]
}
Now I want to do the same thing like Postman but in python, I'm using the package requests. I can do the GET request. But I don't know how to write the "body script in JSON" in Python, I tried different things but it's not working.
Here is my python script:
Does anyone know a bit about API, Postman and python? I hope I was clear enough... Thank you for your help, or at least thank you for taking the time to read my post!
I first used Postman, and it was very easy to have access to the information:
I told Postman the API address: http://www.adress.com, the API key, and made a GET request to retrieve some information.
Then I used a POST request and had to write a JSON Script in the "Body" section of Postman to have my informations. The following script for example:
{
"structures_name" : [“Ma structure”],
"boitiers_uid" : [“12:34:56:78:90”],
"dates" : ["20160606 06:06","20160707 06:06"],
"IO_name" : ["Mon entrée","Ma sortie"],
"IO_type": ["TEMPERATURE"],
"period_length": ["year","month"]
}
Now I want to do the same thing like Postman but in python, I'm using the package requests. I can do the GET request. But I don't know how to write the "body script in JSON" in Python, I tried different things but it's not working.
Here is my python script:
import requests payload = {'api_key':'API_KEY_REMOVED'} #Requete GET resp = requests.get('http://www.adress.com/api/V1/org', params=payload) print (resp.status_code) print (resp.json()) #Requete POST r = requests.post('http://www.adress.com/api/V1/struct', json=payload) print(r.json()) data = {'structures_names': ["ENERGIE AGENCE - POST LU"], 'dates' : ["2018-07-06 06:06","2018-07-30 06:06"], 'IO_name' : ["temp bureau minerais 1 etage 3"], 'IO_type': ["TEMPERATURE"], 'period_length': ["year","month"]}[/b] r = requests.post('http://www.ewattchcloud.fr/api/V1/struct?api_key=API_KEY_REMOVED', json = data) print (r.status_code) print(r.json())In the POST request, I don't know how to write the "data" so that Python understands what I want to do...
Does anyone know a bit about API, Postman and python? I hope I was clear enough... Thank you for your help, or at least thank you for taking the time to read my post!