Advertisement
CodeCrusader

Make HTTP Request Python

Jan 2nd, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | Source Code | 0 0
  1. # To make an HTTP request in Python, you can use the requests library. This library provides a convenient and easy-to-use interface for sending HTTP requests in Python.
  2.  
  3. # Here is an example of how to send a GET request to retrieve the contents of a webpage:
  4.  
  5. import requests
  6.  
  7. url = 'https://www.example.com/'
  8. response = requests.get(url)
  9.  
  10. print(response.content)
  11.  
  12. # This will print the HTML content of the webpage to the console.
  13.  
  14. # You can also send POST requests, set request headers, and handle other HTTP methods using the requests library. Here is an example of how to send a POST request with a JSON payload:
  15.  
  16. import requests
  17.  
  18. url = 'https://www.example.com/api/create'
  19. headers = {'Content-Type': 'application/json'}
  20. data = {'key': 'value'}
  21.  
  22. response = requests.post(url, headers=headers, json=data)
  23.  
  24. print(response.status_code)
  25.  
  26. # For more information about the requests library, you can refer to the documentation: https://requests.readthedocs.io/en/latest/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement