I am working through the course “Working with APIs” and I am a little confused about the difference between .content and .json(). Do they both output the same thing? Is the difference that .json() is callable and searchable? In other words, I can put in parameters and look within it to find keys?
I’ll assume the lesson teaches requests library since there is no link to mission.
If you want to investigate any object in python, you can start with
type(object) to get a readable interpretation its type and google from there the implementation and practical uses. You can also use
dir(object) to find out what methods an object has.
You can do the above analysis for both the object that contains both .json and .content methods (i suppose its the requests.Response object: https://www.w3schools.com/python/ref_requests_response.asp), or do the above for the objects returned by .json and .content, which will tell you what type it is and what you can do with it.
From the docs, https://requests.readthedocs.io/en/master/user/quickstart/ you can see it says .content is to decode bytes, while .text is to decode strings, and .json is to parse json data into python dictionary. Which type of these a request returns is controlled by the designer of the API endpoint. The designer specifies what route/url you use to get what information formatted in what form.
Thank you for the response Hanqi. This input was helpful. I will also look at the docs more carefully to gain a better understanding of the functions.