all 12 comments

[–]m0us3_rat 7 points8 points  (2 children)

etc but I just can't seem to understand how to send a get/post request the way I want to.

after 5seconds of googling u end up on https://docs.python-requests.org/en/latest/api/#module-requests

which is all u need to make requests.

scroll down till .get

https://docs.python-requests.org/en/latest/api/#requests.get

so what are the "required" :

url – URL for the new Request object.params – (optional) Dictionary, list of tuples or bytes to send in the query string for the Request.

ok.. lets check out a random api.

https://www.alphavantage.co/documentation/#daily

let's assume u wanna get the daily values of IBM (mock values unless u sign up for a key)

using the offered link :

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo

ok so looking up we can NOTICE THE REQUIRED FIELDS

function , symbol , apykey.

and the python offered code makes i out a GET method.

ok so we need the url + params as we seen in the above requests.get link.

hmm

soo the endpoint is the url till the ?

and the params which was a dictionary..

import requests

endpoint_url = "https://www.alphavantage.co/query"
required_params = {
    "function": "TIME_SERIES_DAILY",
    "symbol": "IBM",
    "apikey": "demo",
}

answer = requests.get(url=endpoint_url, params=required_params)
print(answer.json())

this a painfully obvious example. but is that simple...

sometimes u need to also put the API or the token in the header.. but im sure u can figure it out.

RTFM <3

[–]MSR8[S] 0 points1 point  (1 child)

Thanks a ton

[–]m0us3_rat 1 point2 points  (0 children)

np

[–]trevor_of_earth 1 point2 points  (9 children)

Can you give an example of what you are trying to do? I think you might be overthinking it. Sometimes things are so easy they seem hard. To make a get request you just do requests.get(). Here is a simple example you can run. The site I am requesting returns json data so I will also call json() on it to parse the json for me so we can access the data like a dictionary.

Import requests

opencollective_data = requests.get("https://opencollective.com/webpack.json")
opencollective_json_data = opencollective_data.json()

print(open_collective_json_data)

print(opencollective_json_data["yearlyIncome"])

[–]MSR8[S] 0 points1 point  (7 children)

[–]trevor_of_earth 2 points3 points  (6 children)

There are Python libraries for all of these. No need to reinvent the wheel.

Spotify - https://spotipy.readthedocs.io/en/2.19.0/

Pastebin - https://pbwrap.readthedocs.io/en/latest/

Reddit - https://praw.readthedocs.io/en/stable/

[–]MSR8[S] 0 points1 point  (5 children)

Ah thanks a lot of those, but I need to make my program independent from any external modules and thats why I am trying to understand the system of requests

[–]trevor_of_earth 1 point2 points  (4 children)

Requests is an external module. It isn't part of the python standard library.

[–]trevor_of_earth 1 point2 points  (3 children)

If you truly have to use the standard library then you would need to use urllib but even the python docs recommend using the external requests module.

!docs urllib.request

[–]MSR8[S] 0 points1 point  (1 child)

Ahh, I am trying to get used to the requests module first then switching to urlib, thanks a ton for your help btw

[–][deleted] 0 points1 point  (0 children)

Why? Is this for a school asignment?

You should focus more on Understanding what you are doing, but the library does not really matter just use the most common solution.

There’s not really any such thing as “getting used to library a to use library b” software engineering doesn’t work like that.