0

Im currently experiencing a bug caused by not attaching the header Content-Type in a curl request.

With a correct request

curl -v -XPOST -H'Content-Type: image/png' -d@image.png "localhost:9000"

The RequestHandler.request.arguments.items() is a clean list []

However, if I remove the -H'Content-Type: image/png' header, and just do something like:

curl -v -XPOST -d@image.png "localhost:9000"

arguments.items() will be come completely messed up - it now contains a list of random unicodes that looks kind of like this:

[('', ['\x19\xf4cX0\xd4A\x86\xd2\x90\x0e\xcf\xa9p\xe5\x82:\x8c\xa8VJz\xc4\xd8Pa\xbfr\xb9`\xc7\xa4\xa8\x90<\xb5\xe3\xd4\x15\xb8\xb9\xfec.*\xff\xd7\xe0rb\xae\x16\x9a\xf5\x08\x9e\xc5\x7f\xd0\x8b\xdf\x9d\x0fQ2, ......

Which I assume to be the content of the image I've attached.

How can I resolve this problem? Is there certain flag/function/etc that I can use inside tornado that would prevent the data from being lost? Or do I have to make the user to include the 'Content-Type' header?

EDIT:

Sorry that I didn't make this clearer earlier, I want to extract the stuff inside RequestHandler.request.arguments.items(), which may contains some values that I need.

2 Answers2

0

It seems you are trying to upload the image to the website. Content-type is not necessary. You need to send HTTP Multipart Data.

According to this answer, you need to use -F to send multipart data:

curl -F "image=@/home/user1/Desktop/test.jpg" "localhost:9000"

As for tornado part, you can use self.request.files to access the file uploaded. Here's an example.

ljk321
  • 16,242
  • 7
  • 48
  • 60
0

request.arguments is for HTML form arguments. When you use -d@image.png, the data is sent not as form arguments but as a raw body, so you need to read it from request.body instead. This is normally done with method PUT instead of POST.

Alternately, you can change the request to use a multipart form wrapper as in @skyline75489's answer, and read the image from request.files.

Ben Darnell
  • 21,844
  • 3
  • 29
  • 50