Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > HTTP POST File without cURL

Reply
Thread Tools

HTTP POST File without cURL

 
 
John D Giotta
Guest
Posts: n/a
 
      09-09-2009
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 usernameasswd --data-binary @/home/jdgiotta/test.zip -H
"Content-Type: application/zip" https://host/selector

Is there a valid way to do this?

 
Reply With Quote
 
 
 
 
David Stanek
Guest
Posts: n/a
 
      09-09-2009
On Wed, Sep 9, 2009 at 1:57 PM, John D Giotta<(E-Mail Removed)> 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 usernameasswd --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
 
Reply With Quote
 
 
 
 
Jarkko Torppa
Guest
Posts: n/a
 
      09-09-2009
On 2009-09-09, John D Giotta <(E-Mail Removed)> 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 usernameasswd --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
 
Reply With Quote
 
John Giotta
Guest
Posts: n/a
 
      09-11-2009
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTP SOAP/HTTP GET/HTTP POST milan_9211 Software 0 01-10-2011 02:10 PM
how to rewrite a curl request into a NET::HTTP one ? Kad Kerforn Ruby 2 08-28-2010 04:30 PM
post data using curl elrondrules@gmail.com Python 0 02-12-2007 06:48 PM
cURL in ruby? Faster than Net::HTTP? Ben Johnson Ruby 17 08-25-2006 12:45 PM
Curl/Perl http post performanc issue wkhedr Perl Misc 3 08-03-2006 05:01 PM



Advertisments