HTTPS POST in Python for Sending Messages

Jingsong Wang's Avatar

Jingsong Wang

20 Jun, 2009 09:57 AM via web

I'm having trouble doing a HTTPS POST in Python using httplib to POST to the imified api resource.

Code is below:

  import httplib, urllib

  username = 'username'
  password = 'password'

  resource = 'https://www.imified.com/api/bot'

  params = urllib.urlencode({'botkey':'XXXXX-XXXXXX-XXXXXXX' , 'apimethod':'send', 'userkey':'XXXXXX-XXXXXX-XXXXXXX', 'msg':'hello'})

  base64string = base64.encodestring('%s:%s' % (username, password))
  authString = 'Basic %s' % base64string
  headers = {"AUTHORIZATION" : authString}

  conn = httplib.HTTPSConnection('imified.com:443')
  req = conn.request("POST", "/api/bot", params, {})
  response = conn.getresponse()

I end up getting a 405 Method Not Allowed. Any ideas? I don't want to use Google App Engine to use a proxy. This seems like the only way as urllib2 doesn't seem to recognize 'https' URLs. Has anyone used httplib to successfully make the HTTPS call to the resource with basic authentication?

  1. Support Staff 2 Posted by Adam Kalsey on 20 Jun, 2009 02:47 PM

    Adam Kalsey's Avatar

    Any python developers around that can help Jingsong out?

  2. 3 Posted by Jingsong Wang on 20 Jun, 2009 11:21 PM

    Jingsong Wang's Avatar

    I used my browser to request http://imified.com/api/bot and I got a secure connection failed error in FireFox 3.0.11.

    Per my browser:

    imified.com uses an invalid security certificate. The certificate is only valid for www.imified.com (Error code: ssl_error_bad_cert_domain)

    Something wrong with SSL? Is there a way to make the API exposed not through SSL as it's causing me quite the headache..

  3. 4 Posted by Anonymous on 20 Jun, 2009 11:24 PM

    Anonymous's Avatar

    Try https://www.imified.com/api/bot

    // Anthony

  4. 5 Posted by Jingsong Wang on 20 Jun, 2009 11:50 PM

    Jingsong Wang's Avatar

    Yeah, tried that too.

    Here's my modified code (I think I've tried every way to make a HTTPS
    request in Python now, short of using a proxy):

    #### ... data params and auth headers set here ...

    conn = httplib.HTTPSConnection('www.imified.com:443')
    httplib.HTTPSConnection.connect(conn)
    conn.putrequest("POST", "/api/bot")
    conn.putheader("Content-Length", len(base64string))
    conn.putheader("AUTHORIZATION", authString)
    conn.endheaders()
    conn.send(params)
    response = conn.getresponse()

    I still get a 405 Method Not Allowed, and I know that the headers and params
    are sending fine, because at first I left out 'Content-Length', and I got a
    411 Length Required code.

    Any ideas?

  5. Support Staff 6 Posted by Adam Kalsey on 21 Jun, 2009 05:11 PM

    Adam Kalsey's Avatar

    Since there's another thread going about this, I'm closing this one.

    The other thread is at http://help.imified.com/discussions/problems/17-post-not-allowed-at-httpswwwimifiedcomapibot

  6. Adam Kalsey closed this discussion on 21 Jun, 2009 05:11 PM.

Comments are currently closed for this discussion. You can start a new one.