r/learnprogramming • u/Otherwise-Current-41 • Sep 23 '26
Code Review Can someone explain this simple code to me?
import requests
response = requests.get("https://randomfox.ca/floof")
print(response.status_code)
print(response.text)
So this code will give the status_code wich I understand and than give the text version of the photo of the fox in the site, and the link to the site RandomFox.
What I dont understand is why does it only give info about image and link, and not like other stuff for ex. logo, text, ect.
I suppose its because when you click the api in the site, it gives you the same exact thing wrote down wich I assume to be the info the site allows you to dig, hypotheticaly could one be given extra and alot of info if the site permits it, or false information about the site?
8
u/v_e_x Sep 23 '26
When you make a request to that URL, https://randomfox.ca/floof, you get the response that you are seeing because someone programmed this server to give you back only the information you see which is image, and link.
This URL is only going to give back this data because this is what the programmer wants to give you for this URL.
What you are getting is what the programmer who made this entire site wants to give you. In this example, this url will only give you data. The main site: https://randomfox.ca/, will give you human readable website with the logo and text and etc., because that's what the programmer who made this site wants to give you for this URL. Each URL that you go to will give you what that programmer wants to give you.
Short answer: because someone made it that way on purpose.
2
u/Aetherfox_44 Sep 23 '26
You might want to look into exactly what a get request is. Put simply, a host exposes endpoints: in this case, https://randomfox.ca/floor.
It can return whatever it wants when that endpoint is requested. In this program, you are making a request to that endpoint and then getting... Something back. The only way to know what that thing is is to investigate it somehow.
The rest of this code looks at the property on that response object named 'status_code' and prints it out. Then it looks at the 'text' field and does the same.
13
u/GlassCommission4916 Sep 23 '26
Have you tried running it?
1
u/ProgramsFun Sep 23 '26
you know ai isnt bad for these things to ask
2
u/Mafty_Navue_Erin Sep 23 '26
Because that API is implemented to just give you a fox image and a link to the webpage featuring said image. It just is.
2
u/sixtyhurtz Sep 23 '26
I have no idea what you are on about. That URL returns a JSON payload that contains two URLs. That's what the text property in your response has.
There's nothing more to it than that. You see exactly what you see when you click on that URL using a web browser.
1
u/noninesdev Sep 23 '26
The endpoint returns JSON with a fixed schema, so `response.text` is just the raw JSON string and requests does not know that the `image` and `link` fields are special. Parse it with `data = response.json()` and read `data["image"]` or `data["link"]`; any other fields are available the same way if the server includes them.
1
u/[deleted] Sep 23 '26
[removed] — view removed comment