5

I already have binary data read from a file. Most of the examples I see online link directly to the file, and upload the whole file. I am looking how to upload the binary data that I already have from another source via HTTP POST in python.

2 Answers 2

7

Alternatively:

req = urllib2.Request("http://example.com", data, {'Content-Type': 'application/octet-stream'})
urllib2.urlopen(req)

That also shows how you can specify the Content-Type of the data.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure what online examples you're looking at, but urllib2.urlopen takes the data to post as a chunk of data and not a file at all.

3 Comments

I'll have to take a network sniff. The server that I am POST'ing to is issuing a 400 Bad Request when I try that.
Okay, so that example reads the data from a file into png_data. If you already have the data in memory, just use that in the calls to urllib2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.