Testing a RESTful API with POSTs from Python

Viewed 13034

I'm trying to test a RESTful interface I'm working on (I'm using this one: codeigniter-restserver), and I'd like to use Python.

GETs seem to be working okay, but I'm having trouble with POSTs. I'm not asking about the ins & outs of this library, rather I'm just trying to figure out how to test POSTing with Python. This is what I've got:

import httplib, urllib

params = urllib.urlencode({
    'sentence':     'esta es una frase',
    'translation':  'this is a sentence'
})

headers = {
    "Content-type": "application/x-www-form-urlencoded", 
    "Accept":       "text/plain"
}

conn = httplib.HTTPConnection("localhost:80")
conn.request("POST", "/myapp/phrase", params, headers)

response = conn.getresponse()
print response.status, response.reason

data = response.read()
conn.close()

Is this script sufficient as a way to test POSTing? I've seen a lot of requests on SO about people looking for GUI tools to do this (Firefox plugins, etc) but for me the whole point of building a RESTful app in the first place is to have an API I can script to to modify the db quickly. (Populate it with data from a JSON file, whatever.)

Am I on the right track with this Python-based approach?

Thanks

3 Answers
Related