Understanding REST APIs
A REST API is like having a conversation with a web server using the HTTP protocol. You send an HTTP request, and the server replies with an HTTP response.
KEY COMPONENTS IN A REST API INTERACTION ARE
A. HTTP Request: In simple words it means: You Ask for Something
Every request includes:
- Method: What you want to do (
GET
,POST
,PUT
,DELETE
) - URL: The address of the resource (
/users
,/posts/1
) - Headers: Extra info like content type (
application/json
) - Body: Data sent (for
POST
,PUT
, etc.)
Example:
B. HTTP Response: Server Replies
A response includes:
- Status Code: Was it successful? (
200 OK
,404 Not Found
,201 Created
) - Headers: Info about the response
- Body: The actual data (often JSON)
WORKING WITH REST APIs IN PYTHON
What Is urllib
?
urllib
is part of Python’s standard library and includes submodules like:
urllib.request
: for opening and reading URLsurllib.parse
: for encoding query parametersurllib.error
: for handling exceptions
No installation needed — it’s ready to go out of the box!
We shall use this library to work with Rest APIs
Please refer to the following trinket to see an example on GET, POST, PUT, DELETE operations in Python