Search code examples
pythondjangoresthttp-get

how to do http get with django


This should be one of the simplest things, but I can't find how to do it in the documentation. I have found how to do it python lib2, but I would like to do it with django to get back a status code, content, etc.

Something like:

response = httpget('http://my-ulr.com')
print response.status_code

Solution

  • This functionality is not part of Django (since the framework is designed mainly to serve request and not to make them), but Django is just Python - you can use Python's built-in urlib2 library, but I would strongly suggest using the excellent Requests library.

    Requests is really fun to work with, and you can't say that about urlib2. You can even say Requests is quite djangish in its simple beauty, and it's creator Kenneth Reitz is an active member of the Django community. But anyways - djangish or not, it works great and you can use it in any Python code.

    With Requests your example code would look like this:

    import requests
    
    response = requests.get('http://my-ulr.com')
    print(response.status_code)