Content-type的几种常见类型

967 阅读1分钟

application/x-www-form-urlencoded

  1. 浏览器的原生form表单
  2. 提交的数据按照 key1=val1&key2=val2 的方式进行编码,key和val都进行了URL转码
POST [http://www.example.com](http://www.example.com) HTTP/1.1 
Content-Type: application/x-[www-form-urlencoded](http://www-form-urlencoded);charset=utf-8 

title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3

multipart/form-data

常见的 POST数据提交的方式。我们使用表单上传文件时,必须让 form 的 enctype 等于这个值。

<form action="/" method="post" enctype="multipart/form-data">
  <input type="text" name="description" value="some text">
  <input type="file" name="myFile">
  <button type="submit">Submit</button>
</form>

application/json

消息主体是序列化后的 JSON 字符串,这个类型越来越多地被大家所使用。

POST [http://www.example.com](http://www.example.com) HTTP/1.1 
Content-Type: application/json;charset=utf-8 

{"title":"test","sub":[1,2,3]}

text/xml

是一种使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范。

POST [http://www.example.com](http://www.example.com) HTTP/1.1 
Content-Type: text/xml 
<!--?xml version="1.0"?--> 
<methodcall> 
    <methodname>
        examples.getStateName
    </methodname> 
    <params> 
        <param>
            <value>
                <i4>41</i4>
            </value>
        </param>
    </params>
</methodcall>