Chinese data processing tornado form submission

Background Description

The front end through a form submitted data with the Chinese, the back end wants to process the data. request.bodyIs a binaryvariable, although it can be decoded into utf8but still not Chinese form, similar to

%E8%BF%98%E6%AC%BE%E6%88%90%E5%8A%9F%E9%A1%B5

After checked the relevant answer, I find a viable way to look at this record.

Solution

The front end of the HTML file specified formparameters form, as follows:

<form action="/" method="post" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded">
</form>

Back-end postprocess is as follows:

import tornado
import urllib.parse

class MyTestHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('test.html')

    def post(self):
        body = self.request.body.decode('utf8')
        body = urllib.parse.unquote(body)
        print(body)
        # something else
        # self.render('test.html')

My front-end results of the two Chinese text input
Here Insert Picture Description
results are as follows:

文案内容_0=这是一个测试文案&文案内容_1=这是另一个测试文案

Study Notes

Note enctype this argument, as I use that multipart/form-data, but the actual parsing it found that although the Chinese can show similar results:
Here Insert Picture Description

But there are other questions:

  1. the body more than the specified delimiter, such as shape ------WebKitFormBoundarySUFFIX, wherein each of the first post, SUFFIXwill change
  2. If you have content, body will be more than aContent-Disposition: form-data;

In fact, this is a question about the http protocol processing, I have no further information. I find this blog finishing very concise.

Published 120 original articles · won praise 35 · views 170 000 +

Guess you like

Origin blog.csdn.net/u012328476/article/details/104388965