当前位置:网站首页>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 .
边栏推荐
- Embedded classic communication protocol
- Real time data warehouse: meituan's real-time data warehouse construction practice
- They are all talking about Devops. Do you really understand it?
- 结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素
- day7
- Promise从入门到精通 (第1章 Promise的介绍和基本使用)
- Execute add migration migration and report build failed
- 华为APP UI自动化测试岗面试真题,真实面试经历。
- Gbase 8C configuration setting function
- Fiddler mobile packet capturing agent settings (for Huawei glory 60s)
猜你喜欢

清除浮动的原因和六种方法(解决浮动飞起影响父元素和全局的问题)
![[Taichi] draw a regular grid in Tai Chi](/img/48/14e825562afa3ffba96296799617f7.png)
[Taichi] draw a regular grid in Tai Chi

53:第五章:开发admin管理服务:6:开发【admin管理员退出登录,接口】;(一个点:我们想要修改一个采用了某种编码方式的值时,新的值最好也按照这种编码方式编码后,再去修改;)

新零售业态下,零售电商RPA助力重塑增长

FreeRTOS kernel summary

In it, there is a million talent gap, and the salary rises, but it is not capped

Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始

Record a production deadlock

Flex布局—固定定位+流式布局—主轴对齐—侧轴对齐—伸缩比

go 学习02 基础知识
随机推荐
Simplicity for beauty - programming ideas
53:第五章:开发admin管理服务:6:开发【admin管理员退出登录,接口】;(一个点:我们想要修改一个采用了某种编码方式的值时,新的值最好也按照这种编码方式编码后,再去修改;)
Causes and solutions of JS digital accuracy loss
Vxe Table/Grid 单元格分组合并
清除浮动的原因和六种方法(解决浮动飞起影响父元素和全局的问题)
What is method and methodology: understand the underlying logic of self-improvement
Cloud native enthusiast weekly: the evolution of Prometheus architecture
Flex development web page instance web side
N32l43x FLASH read \ write \ erase operation summary
Hcip day 12 notes
synchronized详解
小米网站主页面大模块——小模块+导航(浮动案例)
day7
Zkrollup learning materials summary
Gbase 8C backup control function (I)
Process data and change the name of data
SkyWalking分布式系统应用程序性能监控工具-中
Wechat applet pictures are scaled according to the screen scale
sftp文件/文件夹上传服务器
损失函数-交叉熵的原理及实现