HTTP POST File without cURL

Discussion in 'Python' started by John D Giotta, Sep 9, 2009.

  1. I'm working with an API that allows me to POST a zip file via HTTP and
    the documentation uses a cURL example. cURL works, but when I try to
    POST the file via python it fails.
    I don't want to use cURL (since I'm trying to be transparent and
    dependency-less), but I can't find anything online that works.

    When I use multipart/form-data methods (found here
    http://code.activestate.com/recipes/146306/), the recipient cannot
    decipher the attached file.

    This is about the most difficult thing I've had to do with python and
    yet it is supposed to be the very basics of HTTP.

    Example cURL command:
    curl -v -u username:passwd --data-binary @/home/jdgiotta/test.zip -H
    "Content-Type: application/zip" https://host/selector

    Is there a valid way to do this?
     
    John D Giotta, Sep 9, 2009
    #1
    1. Advertising

  2. John D Giotta

    David Stanek Guest

    On Wed, Sep 9, 2009 at 1:57 PM, John D Giotta<> wrote:
    > I'm working with an API that allows me to POST a zip file via HTTP and
    > the documentation uses a cURL example. cURL works, but when I try to
    > POST the file via python it fails.
    > I don't want to use cURL (since I'm trying to be transparent and
    > dependency-less), but I can't find anything online that works.
    >
    > When I use multipart/form-data methods (found here
    > http://code.activestate.com/recipes/146306/), the recipient cannot
    > decipher the attached file.
    >
    > This is about the most difficult thing I've had to do with python and
    > yet it is supposed to be the very basics of HTTP.
    >
    > Example cURL command:
    > curl -v -u username:passwd --data-binary @/home/jdgiotta/test.zip -H
    > "Content-Type: application/zip" https://host/selector
    >
    > Is there a valid way to do this?
    >


    Without seeing code it is hard to tell what is happening. What I would
    do is capture the HTTP traffic and compare Python to cURL. Then you'll
    know how to change you script.

    --
    David
    blog: http://www.traceback.org
    twitter: http://twitter.com/dstanek
     
    David Stanek, Sep 9, 2009
    #2
    1. Advertising

  3. On 2009-09-09, John D Giotta <> wrote:
    > I'm working with an API that allows me to POST a zip file via HTTP and
    > the documentation uses a cURL example. cURL works, but when I try to
    > POST the file via python it fails.
    > I don't want to use cURL (since I'm trying to be transparent and
    > dependency-less), but I can't find anything online that works.
    >
    > When I use multipart/form-data methods (found here
    > http://code.activestate.com/recipes/146306/), the recipient cannot
    > decipher the attached file.
    >
    > This is about the most difficult thing I've had to do with python and
    > yet it is supposed to be the very basics of HTTP.
    >
    > Example cURL command:
    > curl -v -u username:passwd --data-binary @/home/jdgiotta/test.zip -H
    > "Content-Type: application/zip" https://host/selector
    >
    > Is there a valid way to do this?


    Maybe, but reading from curl manpage it seems that that is doing

    POST /selector ...
    Content-type: application/zip

    data


    The backend is broken, they should have used PUT for that. Search
    for "python http put" and adapt, or lookt at urllib(2)/httplib and
    roll your own.

    --
    Jarkko Torppa
     
    Jarkko Torppa, Sep 9, 2009
    #3
  4. John D Giotta

    John Giotta Guest

    Is there a verbose feature for urllib2.urlopen?

    Here is my python snippet for posted the file:

    req = urllib2.Request(url='https://%s%s' % (host, selector),
    data=open('test.zip', 'rb').read())
    req.add_header('content-type', 'application/zip')
    req.add_header('Authorization', 'Basic %s' % self.auth)
    #req.add_header('content-length', str(len(body)))
    print req.headers
    u = urllib2.urlopen(req)
     
    John Giotta, Sep 11, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. post data using curl

    , Feb 12, 2007, in forum: Python
    Replies:
    0
    Views:
    393
  2. Ben Johnson

    cURL in ruby? Faster than Net::HTTP?

    Ben Johnson, Aug 23, 2006, in forum: Ruby
    Replies:
    17
    Views:
    375
    Francis Cianfrocca
    Aug 25, 2006
  3. gberz3
    Replies:
    0
    Views:
    159
    gberz3
    Jun 23, 2008
  4. Kad Kerforn
    Replies:
    2
    Views:
    145
    Kad Kerforn
    Aug 28, 2010
  5. wkhedr

    Curl/Perl http post performanc issue

    wkhedr, Aug 3, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    341
    wkhedr
    Aug 3, 2006
Loading...

Share This Page