当前位置:网站首页>Four common post data submission methods
Four common post data submission methods
2022-07-28 02:12:00 【Xiao Liu xuezhuo】
HTTP/1.1 agreement Stipulated HTTP The request method is OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACE、CONNECT These kinds of . among POST It is generally used to submit data to the server , This article mainly discusses POST Several ways to submit data .
We know ,HTTP The agreement is based on ASCII Code transmission , Based on the TCP/IP The application layer specification above the protocol . To regulate HTTP The request is divided into three parts : Status line 、 Request header 、 Message body . Similar to the following :
Provisions of the agreement POST The submitted data must be placed in the message body (entity-body) in , But the protocol doesn't specify what encoding method the data must use . actually , Developers can decide the format of the message body , Just send the last HTTP The request satisfies the above format .
however , Data sent out , Only when the server resolves successfully can it make sense . General server-side languages such as PHP、Python etc. , And their framework, Both have built-in functions to automatically parse common data formats . The server is usually based on the request header (headers) Medium Content-Type Field to know how the message body in the request is encoded , Then analyze the subject . So when it comes to POST Submit data plan , Contains Content-Type And message body coding . Let's start with them .
application/x-www-form-urlencoded
This should be the most common POST The way to submit data . Browser native form Forms , If not set enctype attribute , Then in the end application/x-www-form-urlencoded How to submit data . The request is similar to the following ( Irrelevant request headers are omitted in this article ):
First ,Content-Type Is specified as application/x-www-form-urlencoded; secondly , Data submitted as per key1=val1&key2=val2 The way to code ,key and val It's all going on URL transcoding . Most server-side languages have good support for this approach . for example PHP in ,$_POST['title'] Can be obtained title Value ,$_POST['sub'] You can get sub Array .
A lot of times , We use it Ajax When submitting data , It's the same way . for example JQuery and QWrap Of Ajax,Content-Type The default values are 「application/x-www-form-urlencoded;charset=utf-8」.
multipart/form-data
This is another common POST How to submit data . When we use forms to upload files , Must let form Of enctyped Equal to this value . Let's take a look at a request example :
This example is a little more complicated . First, a boundary Used to split different fields , In order to avoid the repetition of the text ,boundary It's long and complicated . then Content-Type The data is indicated in mutipart/form-data Encoding , The purpose of this request is boundary What is it . The message body is divided into several parts with similar structure according to the number of fields , Every part is based on --boundary Start , And then the content description information , And then there's the carriage return , Finally, the specific content of the field ( Text or binary ). If you are transferring files , Also include file name and file type information . The message body ends with --boundary-- Mark the end . About mutipart/form-data The detailed definition of , Please go to rfc1867 see .
This method is usually used to upload files , The major server languages also have good support for it .
These two kinds mentioned above POST The way of data , It's all browser native support , And at this stage of the original form Forms also Only two ways are supported . But as more and more Web Site , In especial WebApp, All use Ajax After data interaction , We can define a new way to submit data , Bring more convenience to development .
application/json
application/json This Content-Type As a response leader, you must be familiar with . actually , Now more and more people use it as a request header , Used to tell the server that the message body is serialized JSON character string . because JSON The popularity of norms , Except for the lower version IE All other browsers support native JSON.stringify, The server language also has processing JSON Function of , Use JSON No trouble .
JSON Formats support much more complex structured data than key value pairs , It's also useful . Remember when I was working on a project a few years ago , The level of data that needs to be submitted is very deep , I just put the data JSON It was submitted after serialization . But at that time, I put JSON String as val, Still in the key value pair , With x-www-form-urlencoded Mode submission .
Google Of AngularJS Medium Ajax function , The default is to submit JSON character string . For example, the following code :
The final request sent is :
POST http://www.example.com HTTP/1.1
Content-Type: application/json;charset=utf-8
{ "title":"test","sub":[1,2,3]}
This program , Can easily submit complex structured data , Perfect for RESTful The interface of . All kinds of bag grabbing tools are as follows Chrome Built in developer tools 、Firebug、Fiddler, Will be displayed in a tree structure JSON data , Very friendly . But there are also some server languages that don't support this approach , for example php Can't go through $_POST Object gets the content from the request above . Now , You need to do it yourself : In the request header Content-Type by application/json when , from php://input Get the original input stream from the , Again json_decode Object . some php The framework has started to do that .
Of course AngularJS It can also be configured to use x-www-form-urlencoded How to submit data . If necessary , You can refer to This article .
text/xml
Before my blog Mentioned XML-RPC(XML Remote Procedure Call). It's a use HTTP As a transport protocol ,XML The remote call specification as an encoding . Typical XML-RPC This is the request :
XML-RPC Simple protocol 、 Sufficient functions , There are various language implementations . It is also widely used , Such as WordPress Of XML-RPC Api, Search engines ping service wait .JavaScript in , Also have Off the shelf Library Support data interaction in this way , Can support the existing XML-RPC service . however , Personally, I think XML The structure is still too bloated , For general scenes JSON It will be more flexible and convenient .
边栏推荐
- Promise从入门到精通(第4章 async 和 await)
- Five basic data structures of redis
- 华为APP UI自动化测试岗面试真题,真实面试经历。
- Fiddler mobile packet capturing agent settings (for Huawei glory 60s)
- Real time data warehouse: meituan's real-time data warehouse construction practice
- 软件测试面试题:你认为做好测试用例设计工作的关键是什么?
- Lightweight project management system
- [website construction] update SSL certificate with acme.sh: change zerossl to letsencrypt
- mongodb/mongoTemplate.upsert批量插入更新数据的实现
- UE4 unreal ndisplay plug-in easy to use three fold screen details
猜你喜欢
![Likeshop takeout ordering system [100% open source, no encryption]](/img/e6/a73aa817b5b30339d755aa53708072.png)
Likeshop takeout ordering system [100% open source, no encryption]

JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method

ArcGIS:加载历史遥感影像

Promise从入门到精通 (第2章 Promise的理解和使用)

Completely delete MySQL in Linux system

Codeworks round 807 (Div. 2) a-c problem solution

IT这个岗位,人才缺口百万,薪资水涨船高,上不封顶

Five basic data structures of redis

QGIS制图:矢量数据制图流程及导出

Digital economy is the core of future economic development
随机推荐
Promise从入门到精通 (第1章 Promise的介绍和基本使用)
ArcGIS:加载历史遥感影像
day7
Forget the root password
软件测试面试题:为什要在一个团队中开展测试工作?
数据输出-绘制动图
A letter to the user of qubu drawing bed
Use of classes in typescript
都在说DevOps,你真正了解它吗?
Flex layout learning completed on PC side
Simplicity for beauty - programming ideas
Synchronized details
focal loss原理及实现
A lock faster than read-write lock. Don't get to know it quickly
mysql创建存储过程---------[HY000][1418] This function has none of DETERMINISTIC, NO SQL
微信小程序图片根据屏幕比例缩放
Solution of digital commerce cloud supply chain centralized purchase management system: centralized purchase system management mode, digital control of enterprise materials
【Star项目】小帽飞机大战(六)
Software test interview questions: common post data submission methods
Gbase 8C annotation information function