Introduction
In this lab, you will dive into the underwater world as a scuba diver in search of hidden treasures. Your goal is to learn how to make HTTP requests in Python to fetch data from the depths of the internet ocean.
Making a GET Request
In this step, you will learn how to make a GET request using Python's requests library. You will send a GET request to a remote server and retrieve the response data.
- Open a Python file named
/home/labex/project/get_request.py. - Write the following code in
get_request.py:
import requests
response = requests.get('https://labex.io/api/v2/vm')
print(response.text)
Run the script:
python get_request.py
The information below should be displayed on your terminal:
{"code":401, "reason":"UNAUTHORIZED", "message":"Please login and try again", "metadata":{}}
Making a POST Request
In this step, you will learn how to make a POST request using Python's requests library. You will send a POST request to a remote server and observe the response.
- Open a new Python file named
/home/labex/project/post_request.py. - Write the following code in
post_request.py:
import requests
data = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://labex.io/api/v2/vm', data=data)
print(response.text)
Run the script:
python post_request.py
The information below should be displayed on your terminal:
{"code":401, "reason":"UNAUTHORIZED", "message":"Please login and try again", "metadata":{}}
Summary
In this lab, you learned how to make HTTP requests in Python using the requests library. By practicing GET and POST requests, you have gained valuable skills in fetching data from remote servers and interacting with web APIs. This knowledge will allow you to explore the vast world of web-based resources and integrate data into your Python projects effectively.



