Skip to content

Posting to Koornk using Python

While building something nice on Koornk (Slovenian Twitter like service), I stopped for a few months to learn how to do Authenticated POST requests using Python. I found urllib2 way too complicated, but soon I stumbled across great Yahoo Developer page – Make Yahoo! Web Service REST calls with Python that also lists alternative approach using httplib2 which then works beautifully.

Koornk blurry t-shirt
Image by Gandalfar via Flickr

Here’s the snippet of code used:

import urllib
import httplib2
http = httplib2.Http()

url = ‘http://www.koornk.com/api/update/’
username = ‘USERNAME’
password = ‘PASSWORD’

http.add_credentials(username, password)

params = urllib.urlencode({‘status’: ‘ hey, im testing koornk api – httplib2 python request’})

response, content = http.request(url, ‘POST’, params,
headers={‘Content-type’: ‘application/x-www-form-urlencoded’})

import simplejson
from pprint import pprint
s = simplejson.loads(content)
pprint(s)

Reblog this post [with Zemanta]

6 responses to “Posting to Koornk using Python

Comments are closed.