Python Requests

High-level library to interact with HTTP servers.

Based on the library urllib3.

Why use?

  • Keep-Alive and connection pooling.

  • Sessions with cookie persistence.

  • Digest/Basic authentication.

  • File upload using Multipart.

GET

import requests

if __name__ == "__main__":
    r = requests.get("https://www.ua.pt/manifest.json")
    print(r.text)

Or better.

if __name__ == "__main__":
    r = requests.get("https://www.ua.pt/manifest.json")
    data = r.json()
    print(data["name"])

POST

if __name__ == "__main__":
    url = "https://httpbin.org/post"
    payload = {"name": "Diogo"}
    response = requests.post(url, json = payload)
    print(response.text)

POST of a file

if __name__ == "__main__":
    url = "https://httpbin.org/post"
    files = {'file': open('movie.mp4', 'rb')}

    r = requests.post(url, files=files)
    print(r.text)

Last updated