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.
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.
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.
/home/labex/project/get_request.py
.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":{}}
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.
/home/labex/project/post_request.py
.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":{}}
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.