0

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!

mathmonkey
  • 335
  • 4
  • 15

1 Answers1

1

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 is a lot easier than urllib2, especially when POSTing data.

Thomas Fenzl
  • 4,342
  • 1
  • 17
  • 25
  • Thanks, I've recompiled Python with ssl. Sticking with urllib2 for the moment though, how do I POST a new document to my collection with mongolab REST API? – mathmonkey Jun 29 '13 at 17:59
  • How to post with urllib2: http://stackoverflow.com/questions/3238925/python-urllib-urllib2-post – Thomas Fenzl Jun 29 '13 at 18:15