Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making an API post
#1
Smile 
I have been trying to learn Python formatted as Json. For the primary purpose of home automation. I have learned to say "Hello world " a few different ways. I have learned some IF's and ELIF, while, Inputs, Time.sleep. However this does not help me with the original task. Making a simple API request. A post to trigger webhook. I can simply type it into Google and it will trigger my webhook which in turn controls the device. Yet I cannot find anywhere to explain how to do this with Python. I am attempting a simple code of IF this input is true post this API or webhook. Every time I try to find this it leads me down a rabbit hole of you need "this" (some format of code or library or something) which leads to oh well for that you need "this" (another form of code, download, attachment etc..) For example I downloaded something called "requests" it was supposed to be a download for making HTTP requests. Then I go to use it and my pycharm doesn't recognize it. I get an error saying that it is for an older version of pycharm.

Can anyone help me find a place or way to explain these API to me?

Thank you for your time and help.
python_moron
buran write Apr-08-2021, 08:21 AM:
removed color and small size - it's hard on eyes
Reply
#2
Run this command
python -m pip install requests
this will install requests lib.

Make a POST request to a web page, and return the response text:
import requests

url = 'https://www.w3schools.com/python/demopage.php'
myobj = {'somekey': 'somevalue'}

x = requests.post(url, data = myobj)

print(x.text)
Reply
#3
(Apr-08-2021, 08:09 AM)python_moron Wrote: I have been trying to learn Python formatted as Json.
python formatted as JSON?

(Apr-08-2021, 08:09 AM)python_moron Wrote: Making a simple API request.
There are two possible options - either there is some package/wrapper already available for your particular IoT device(s) (e.g. provided by the manufacturer and/or enthusiasts) or you need to work with native/general package like requests and simply make the post request.
In both cases we can little to help (mostly general advice like use requests), given that we don't have any information on device or the API.

(Apr-08-2021, 08:09 AM)python_moron Wrote: For example I downloaded something called "requests" it was supposed to be a download for making HTTP requests. Then I go to use it and my pycharm doesn't recognize it. I get an error saying that it is for an older version of pycharm.
This indicates that you have trouble installing requests and more importantly - you are confused about how to install packages in general. I don't think the error says that requests is for older version of pycharm. pycharm is IDE and requests does not care what IDE you use. Most likely the error says you downloaded a version for different bersion of python. BUT you don't have to download it, but install it via pip. So let's start form here - provide more information on the error you get (i.e. the full error message/traceback) you get. Also - what OS, what version of python, etc.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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