Uploading an image using PUT

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • noelob

    Uploading an image using PUT

    Hi All,

    Let me start by saying that's I'm relatively new to Python, so please
    be gentle!

    I need to up upload a file to a Tomcat web app using httplib. The web
    app requires the following:
    Files need to be split into 100kb (102400b) and each file segment
    loaded using the PUT request. It is also a requirement that the
    following headers be sent:

    For simplicity I've used an image (jpg) smaller than 100kb, so it
    doesn't need to be chunked

    headers = {
    "Accept": "text/xml",
    "Authorization" : "testAuthHeader ",
    "Content-Length": 50172,
    "Content-Range": "bytes 0-50172/50172",
    "Content-Type": "image/jpeg",
    "If-Match": "1",
    "User-Agent": "(en-IE; Grinder)",
    }

    I make the following connection:

    conn = httplib.HTTPCon nection(url)
    conn.request(me thod, uriStr, body, additionalHeade rs)

    where:
    method = PUT
    uriStr is the web app specific URI
    body is the raw data (bytes) from the jpg image
    additionalHeade rs are the headers above

    For some reason I keep getting a Status 400 error from the web server.
    Is it possible to PUT (or POST?) data in this fashion using httplib?

    Many thanks for your help
    Noelob
  • noelob

    #2
    Re: Uploading an image using PUT

    Hi All,
    I finally figured it out, thanks to Wireshark! I was adding my own
    "Content-Length: 50173" header to the request, but httplib appears to
    add its own header (also "Content-Length: 50173") which must have been
    causing a conflict and making the sever return "HTTP/1.1 400 Bad
    Request\r\n". As soon as I removed my own Content-Length header, it
    worked!

    Regards,
    Noel

    Comment

    Working...