Python mongolab REST api
Question: I am trying to access the mongolab REST api through python. Is the correct way
to do this via pythons urllib2? I have tried the following:

    
    
    import urllib2
    
    p = urllib2.urlopen("https://api.mongolab.com/api/1/databases/mydb/collections/mycollection?apiKey=XXXXXXXXXXXXXXXX")
    

But this gives me an error:

    
    
    urllib2.URLError: <urlopen error unknown url type: https>
    

What is the correct way of doing this? After connecting, how do I go on to
POST a document to my collection? If someone could post up a code example, I
would be very grateful. Thanks all for the help!

EDIT:

I've recompiled python with ssl support. How do I POST insert a document to a
collection using mongolab REST API? Here is the code I have atm:

    
    
    import urllib
    import urllib2
    
    url = "https://api.mongolab.com/api/1/databases/mydb/collections/mycollection?apiKey=XXXXXXXXXXXXXXXX"
    data = {"x" : "1"}
    request = urllib2.Request(url, data)
    p = urllib2.urlopen(request)
    

Now, when I run this, I get the error

    
    
    urllib2.HTTPError: HTTP Error 415: Unsupported Media Type
    

How do I insert documents using HTTP POST? Thanks!


Answer: That error is raised if you version of python does not include ssl support.
What version are you using? Did you compile it yourself?

That said, when you get a version including ssl, using
[requests](https://requests.readthedocs.org/en/v1.1.0/) is a lot easier than
urllib2, especially when POSTing data.

