Skip to content

Instantly share code, notes, and snippets.

@Eleonore9
Last active January 26, 2016 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eleonore9/70351cd0548ee1704d6a to your computer and use it in GitHub Desktop.
Save Eleonore9/70351cd0548ee1704d6a to your computer and use it in GitHub Desktop.
Example of GET request in Python

Basic GET request in Python:

Replace username and password by your own credentials. Replace url by the url of your API of interest.

import sys
import urlparse
import requests
from requests.auth import HTTPBasicAuth

auth = HTTPBasicAuth(username, password)
    try:
        r = requests.get(url=url, auth=auth)
        if r.status_code == requests.codes.ok:
            print ">> Your http request was accepted! =)"
            return r.json()
        else:
            print ">> Something went wrong! :-/"
            r.raise_for_status()
    except requests.exceptions.ConnectionError as e:
        print ">> Connection error: ", e.message
    except requests.exceptions.ConnectTimeout as e:
        print ">> Connection timed out: ", e.message
    except requests.exceptions.HTTPError as e:
        print ">> HTTP error: ", e.message
    except requests.exceptions.RequestException as e:
        print ">> Request exception: ", e.message
        sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment