Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
API in Python and Postman
#1
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" : ["2016­06­06 06:06","2016­07­07 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!
Reply


Messages In This Thread
API in Python and Postman - by melo - Aug-28-2018, 08:38 AM
RE: API in Python and Postman - by gontajones - Aug-28-2018, 07:48 PM
RE: API in Python and Postman - by melo - Aug-29-2018, 08:17 AM
RE: API in Python and Postman - by gontajones - Aug-29-2018, 10:39 AM
RE: API in Python and Postman - by snippsat - Aug-29-2018, 12:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  upload image with generated code from Postman does not work magic7598 0 1,743 Nov-30-2019, 10:32 AM
Last Post: magic7598
  [split] API in Python and Postman fioranosnake 26 8,593 Nov-20-2019, 07:30 PM
Last Post: fioranosnake

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020