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:
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!
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import requests payload = { 'api_key' : 'API_KEY_REMOVED' } #Requete GET print (resp.status_code) print (resp.json()) #Requete POST 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] print (r.status_code) print (r.json()) |
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!