随笔十四 :请求头中的Content-type含义及应用场景
http请求头:content-type请求体数据格式
-Form提交
content-type:application/x-www-form-urlencoded
数据格式:username=alex&password=dsb
-JSON
content-type:application/json
数据格式:{"username":"alex"}
服务端读取content-type判定用户提交的请求体是什么格式
eg:
1、向知乎发送GET请求https://www.zhihu.com/api/v3/oauth/sms/supported_countries
本质:send后全是请求头的信息
创建socket客户端方法 url 协议版本 host地址 请求头 请求体格式 请求体
socket.send("GET/api/v3/oauth/sms/supported_countrieshttp1.1\r\n host:www.zhihu.com\r\n user-agent:设备号\r\ncontent-type\r\n\r\n ")
2、向知乎发送POST请求https://www.zhihu.com/api/v3/oauth/sms/supported_countries
本质:send后全是请求头的信息
创建socket客户端方法 url 协议版本 host地址 请求头 请求体格式 请求体
socket.send("POST/api/v3/oauth/sms/supported_countrieshttp1.1\r\n host:www.zhihu.com\r\n user-agent:设备号\r\ncontent-type\r\n\r\nusername=alex&password=dsb")
[如果知乎使用Django开发的:]
1、读取reqeust.body,读取原始数据:username=alex&password=dsb
2、django读取Content-Type请求头,如果请求头等于 application/x-www-form-urlencoded,那么的讲哦就会解析request.body中数据,
生成一个QueryDict对象(字典),然后将字典赋值给reqeust.POST
request.POST.get('username')
页:
[1]