一直用AFN 卻沒詳細(xì)總結(jié)過HTTP Content-Type,再次記錄一下;
一. Content-Type 和 Accept
1.1 Accept屬于請(qǐng)求頭秸弛, Content-Type屬于實(shí)體頭梅誓。
Http報(bào)頭分為通用報(bào)頭睛榄,請(qǐng)求報(bào)頭娜汁,響應(yīng)報(bào)頭和實(shí)體報(bào)頭精置。
請(qǐng)求方的http報(bào)頭結(jié)構(gòu):通用報(bào)頭|請(qǐng)求報(bào)頭|實(shí)體報(bào)頭
響應(yīng)方的http報(bào)頭結(jié)構(gòu):通用報(bào)頭|響應(yīng)報(bào)頭|實(shí)體報(bào)頭
1.2 Accept代表發(fā)送端(客戶端)希望接受的數(shù)據(jù)類型统翩。
比如:Accept:text/xml;
代表客戶端希望接受的數(shù)據(jù)類型是xml類型
1.3 Content-Type代表發(fā)送端(客戶端|服務(wù)器)發(fā)送的實(shí)體數(shù)據(jù)的數(shù)據(jù)類型仁锯。
二. Content-Type:
Content-Type用于指定內(nèi)容類型耀找,一般是指網(wǎng)頁中存在的Content-Type,Content-Type屬性指定請(qǐng)求和響應(yīng)的HTTP內(nèi)容類型业崖。如果未指定 ContentType野芒,默認(rèn)為text/html。
在nginx中有個(gè)配置文件mime.types双炕,主要是標(biāo)示Content-Type的文件格式狞悲。
下面是幾個(gè)常見的Content-Type:
1.text/html
2.text/plain
3.text/css
4.text/javascript
5.application/x-www-form-urlencoded
6.multipart/form-data
7.application/json
8.application/xml
...
前面幾個(gè)都很好理解,都是html妇斤,css摇锋,javascript的文件類型,后面四個(gè)是POST的發(fā)包方式站超。
2.1.application/x-www-form-urlencoded
application/x-www-form-urlencoded是常用的表單發(fā)包方式荸恕,普通的表單提交,或者js發(fā)包死相,默認(rèn)都是通過這種方式融求,
比如一個(gè)簡單地表單:
<form enctype="application/x-www-form-urlencoded" action="http://homeway.me/post.php" method="POST">
<input type="text" name="name" value="homeway">
<input type="text" name="key" value="nokey">
<input type="submit" value="submit">
</form>
那么服務(wù)器收到的raw header會(huì)類似:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,gl;q=0.2,de;q=0.2
Cache-Control:no-cache
Connection:keep-alive
Content-Length:17
Content-Type:application/x-www-form-urlencoded
那么服務(wù)器收到的raw body會(huì)是,name=homeway&key=nokey算撮,在php中生宛,通過$_POST就可以獲得數(shù)組形式的數(shù)據(jù)县昂。
2.2 multipart/form-data
multipart/form-data用在發(fā)送文件的POST包。
這里假設(shè)我用python的request發(fā)送一個(gè)文件給服務(wù)器:
data = {
"key1": "123",
"key2": "456",
}
files = {'file': open('index.py', 'rb')}
res = requests.post(url="http://localhost/upload", method="POST", data=data, files=files)
print res
通過工具陷舅,可以看到我發(fā)送的數(shù)據(jù)內(nèi)容如下:
POST http://www.homeway.me HTTP/1.1
Content-Type:multipart/form-data; boundary=------WebKitFormBoundaryOGkWPJsSaJCPWjZP
------WebKitFormBoundaryOGkWPJsSaJCPWjZP
Content-Disposition: form-data; name="key2"
456
------WebKitFormBoundaryOGkWPJsSaJCPWjZP
Content-Disposition: form-data; name="key1"
123
------WebKitFormBoundaryOGkWPJsSaJCPWjZP
Content-Disposition: form-data; name="file"; filename="index.py"
這里Content-Type告訴我們倒彰,發(fā)包是以multipart/form-data格式來傳輸,另外蔑赘,還有boundary用于分割數(shù)據(jù)狸驳。
當(dāng)文件太長,HTTP無法在一個(gè)包之內(nèi)發(fā)送完畢缩赛,就需要分割數(shù)據(jù),分割成一個(gè)一個(gè)chunk發(fā)送給服務(wù)端撰糠,
那么--用于區(qū)分?jǐn)?shù)據(jù)快酥馍,而后面的數(shù)據(jù)就是標(biāo)示區(qū)分包作用。
2.3 text/xml
微信用的是這種數(shù)據(jù)格式發(fā)送請(qǐng)求的阅酪。
POST http://www.homeway.me HTTP/1.1
Content-Type: text/xml
<?xml version="1.0"?>
<resource>
<id>123</id>
<params>
<name>
<value>homeway</value>
</name>
<age>
<value>22</value>
</age>
</params>
</resource>
php中$_POST只能讀取application/x-www-form-urlencoded
數(shù)據(jù)旨袒,$_FILES只能讀取multipart/form-data類型數(shù)據(jù),
那么术辐,要讀取text/xml格式的數(shù)據(jù)砚尽,可以用:
$file = fopen('php://input', 'rb');
$data = fread($file, length);
fclose($file);
或者
$data = file_get_contents('php://input');
2.4 application/json
通過json形式將數(shù)據(jù)發(fā)送給服務(wù)器,一開始辉词,我嘗試通過curl必孤,給服務(wù)器發(fā)送application/json格式包,
然而我收到的數(shù)據(jù)如下:
---------e1e1406176ee348a Content-Disposition: form-data; name="nid" 2---------------
---------e1e1406176ee348a Content-Disposition: form-data; name="uuid" cf9dc994-a4e7-3ad6-bc54-41965b2a0dd7-----------
----------e1e1406176ee348a Content-Disposition: form-data; name="access_token" 956731586df41229dbfec08dd5d54eedb98d73d2----------
----------e1e1406176ee348a--
HTTP通信中并不存在所謂的json瑞躺,而是將string轉(zhuǎn)成json罷了敷搪,也就是,application/json可以將它理解為text/plain幢哨,普通字符串赡勘。