0

I'm tying to write some simple app on python3 and tornado for server, and requests for client, and I'm getting some headers in 'self.request.body', which I can't dispose of. For instance, for file 'blahblahblah', I get:

--cb5f6ba84bdf42d382dfd3204f6307c7\r\nContent-Disposition: form-data; name="file"; filename="1.bin"\r\n\r\nblahblahblah\n\r\n--cb5f6ba84bdf42d382dfd3204f6307c7--\r\n

Files are sent by

f = {'file': open(FILE, 'rb')}
requests.post(URL_UPLOAD, files=f)

and received by

class UploadHandler(tornado.web.RequestHandler):
    def post(self, filename):
        with open(Dir + filename, 'wb') as f:
            f.write(self.request.body)

My full code can be seen here

When I send the file by curl with curl -X POST -d $(cat ./1.bin) http://localhost:8080/upload/1.bin I get the correct file, but without \n.

There must be something I missed. Please can someone help me with that? Thank You.

Sam Tyson
  • 4,239
  • 4
  • 23
  • 33

1 Answers1

0

There are two ways to upload files: simply using the file as the request body (usually, but not necessarily, with the HTTP PUT method), or using a multipart wrapper (usually with the HTTP POST method). If you upload the file from an HTML form, it will usually use the multipart wrapper. Your requests example is using a multipart wrapper and the curl one is not; your server is not expecting the wrapper.

To use a multipart wrapper: in requests, pass files= as you've done here. With curl, see this answer: Using curl to upload POST data with files. On the server, use self.request.files instead of self.request.body: http://www.tornadoweb.org/en/stable/httpserver.html#tornado.httpserver.HTTPRequest.files

To not use the multipart wrapper, use data=open(FILE, 'rb').read() from requests, and keep the other two components the same.

It is possible to support both styles simultaneously on the server: use self.requests.files when self.request.headers['Content-Type'] == 'multipart/form-data' and self.request.body otherwise.

Community
  • 1
  • 1
Ben Darnell
  • 20,778
  • 3
  • 24
  • 41