views:

45

answers:

0

I'm trying to send a POST message with a purely XML payload (I think) using urllib2 in IronPython. However, everytime I send it, it returns Error code 400 (Bad Request).

I'm actually trying to mimick a Boxee remove queue item call for which the actual data packets looks like this (from WireShark):

POST /action/add HTTP/1.1
User-Agent: curl/7.16.3 (Windows  build 7600; en-US; beta) boxee/0.9.21.11487
Host: app.boxee.tv
Accept: */*
Accept-Encoding: deflate, gzip
Cookie: boxee_ping_version=9; X-Mapping-oompknoc=76D730BC9E858725098BF13AEFE32EB5; boxee_app=e01e36e85d368d4112fe4d1b6587b1fd
Connection: keep-alive
Content-Type: text/xml
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: en-us,en;q=0.5
Keep-Alive: 300
Connection: keep-alive
Content-Length: 53

<message type="dequeue" referral="3102296"></message>

I'm using the following python code to send the POST:

def PostProtectedPage(theurl, username, password, postdata):

    req = urllib2.Request(theurl, data=postdata)
    req.add_header('Content-Type', 'text/xml')
    try:
        handle = urllib2.urlopen(req)
    except IOError, e:                  # here we are assuming we fail
        pass
    else:                               # If we don't fail then the page isn't protected
        print "This page isn't protected by authentication."
        sys.exit(1)

    if not hasattr(e, 'code') or e.code != 401:                 # we got an error - but not a 401 error
        print "This page isn't protected by authentication."
        print 'But we failed for another reason.'
        sys.exit(1)

    authline = e.headers.get('www-authenticate', '')                # this gets the www-authenticat line from the headers - which has the authentication scheme and realm in it
    if not authline:
        print 'A 401 error without an authentication response header - very weird.'
        sys.exit(1)

    authobj = re.compile(r'''(?:\s*www-authenticate\s*:)?\s*(\w*)\s+realm=['"](\w+)['"]''', re.IGNORECASE)          # this regular expression is used to extract scheme and realm
    matchobj = authobj.match(authline)
    if not matchobj:                                        # if the authline isn't matched by the regular expression then something is wrong
        print 'The authentication line is badly formed.'
        sys.exit(1)
    scheme = matchobj.group(1) 
    realm = matchobj.group(2)
    if scheme.lower() != 'basic':
        print 'This example only works with BASIC authentication.'
        sys.exit(1)

    base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
    authheader =  "Basic %s" % base64string
    req.add_header("Authorization", authheader)
    try:
        handle = urllib2.urlopen(req)
    except IOError, e:                  # here we shouldn't fail if the username/password is right
        print "It looks like the username or password is wrong."
        print e
        sys.exit(1)
    thepage = handle.read()
    return thepage

However, whenever I run this, it returns Error 400 (Bad Request)
I know the authentication is correct because I use it elsewhere to fetch the queue (and I can't imagine it's not used, otherwise how would it now which account to apply the change to?)

Looking at the network capture, could I simply be missing adding some headers to the request? Probably something simple, but I just don't know enough about python or HTTP requests to know what's what.

Edit: BTW, I'm calling the code as follows (it's actually dynamic, but this is the basic idea):

PostProtectedPage("http://app.boxee.tv/action/add", "user", "pass", "<message type=\"dequeue\" referral=\"3102296\"></message>")