application/x-www-form-urlencoded
form数据被编码为名称/值对。这是标准的编码格式。
即: form数据会被encode成键值对id=123&name='xiaoming' , 与url通过?拼接.
转义:
- 空格转义为+ ;
- 保留字符转移参见RFC1738的第2.2节;
- 非字母数字字符替换为%HH, 即对应的ascii的十六进制形式;
- 换行字符CRLF 转义为 %0D%0A
-
名称与值通过=连接, 名称值对之间使用&连接.
参见: https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
对于python3 , 使用urllib3时, 需要使用 from urllib.parse import urlencode , encoded_args = urlencode(startfields)
application/json
使用json的特点是, post的数据可以有层级嵌套结构 .
后台需要使用 @RequestBody 注解来获取.
前端发送时, 像urllib3 , 需要进行json编码然后放到body中,
>>> import json
>>> data = {'attribute': 'value'}
>>> encoded_data = json.dumps(data).encode('utf-8')
>>> r = http.request(
... 'POST',
... 'http://httpbin.org/post',
... body=encoded_data,
... headers={'Content-Type': 'application/json'})
>>> json.loads(r.data.decode('utf-8'))['json']
{'attribute': 'value'}
multipart/form-data
用来上传包含文件, non-ascii数据和二进制数据的表单.
在表单中使用Content-Dispositon 来进行分割表单的各个部分 :
The following example illustrates "multipart/form-data" encoding. Suppose we have the following form:
<FORM action="http://server.com/cgi/handle"
enctype="multipart/form-data"
method="post">
<P>
What is your name? <INPUT type="text" name="submit-name"><BR>
What files are you sending? <INPUT type="file" name="files"><BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</FORM>
If the user enters "Larry" in the text input, and selects the text file "file1.txt", the user agent might send back the following data:
Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x
Content-Disposition: form-data; name="submit-name"
Larry
--AaB03x
Content-Disposition: form-data; name="files"; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--AaB03x--
If the user selected a second (image) file "file2.gif", the user agent might construct the parts as follows:
Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x
Content-Disposition: form-data; name="submit-name"
Larry
--AaB03x
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=BbC04y
--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary
...contents of file2.gif...
--BbC04y--
--AaB03x--