我想从HTTP Post请求正文中解析表单数据。
HTTP帖子正文如下
-WebKitFormBoundary9L2CBBUR67tDDqbZ Content-Disposition:表单数据; name =“ newRestaurantName”
爸爸琼斯 ------ WebKitFormBoundary9L2CBBUR67tDDqbZ-
我的表单是使用此代码创建的
<html><body><h1>Make a New Restaurant</h1><form method = 'POST' enctype='multipart/form-data' action = '/restaurants/new'><input name = 'newRestaurantName' type = 'text' placeholder = 'New Restaurant Name' > <input type='submit' value='Create'></form></body></html>
这是我的HTTP发布请求处理程序
def do_POST(self):
try:
if self.path.endswith("/restaurants/new"):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length).decode('utf-8')
print(body)
# TODO get restuarant name from body
restuarantName = ""
newRestaurant = Restaurant(name=restuarantName)
session.add(newRestaurant)
session.commit()
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.send_header('Location', '/restaurants')
self.end_headers()
except:
pass
我无法解析HTTP正文,因此无法提取餐厅名称。我尝试使用json.load()和cgi.parse_multipart(self.rfile,pdict)
如何正确执行此操作?