HTTP POST File without cURL

J

John D Giotta

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?
 
D

David Stanek

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.
 
J

Jarkko Torppa

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.
 
J

John Giotta

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top