Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common ways for POST to submit data?

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are the common POST data submission methods". In daily operation, I believe many people have doubts about the common POST data submission methods. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the common POST data submission methods?" Next, please follow the editor to study!

We know that HTTP protocol is an application layer specification based on ASCII code and based on TCP/IP protocol. The specification divides the HTTP request into three parts: the status line, the request header and the message body. Similar to the following form:

The protocol stipulates that the data submitted by POST must be placed in the message body (entity-body), but the protocol does not specify how the data must be encoded. In fact, developers can decide the format of the message body for themselves, as long as the last HTTP request sent meets the above format.

However, if the data is sent out, it will only make sense if the server successfully parses it. General server languages such as php, python, Java, .NET, etc., as well as their framework, have built-in functions to automatically parse common data formats. The server usually knows how the message body in the request is encoded according to the Content-Type field in the request header (headers), and then parses the body. That is, the Content-Type specifies the encoding in the message body. Therefore, POST submits the data scheme, which is directly related to the Content-Type and the message body.

Application/x-www-form-urlencoded

This is the most common way for POST to submit data. The browser's native form form, if you do not set the enctype property, will eventually submit the data as application/x-www-form-urlencoded (enctype's POST default). The request is similar to the following (extraneous request headers are omitted in this article):

POST http://www.example.com HTTP/1.1

Content-Type: application/x-www-form-urlencoded;charset=utf-8

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

First, Content-Type is designated as application/x-www-form-urlencoded

Secondly, the submitted data is encoded in the way of key1=val1&key2=val2, and both key and val are URL transcoded. Most server-side languages support this approach very well. For example, in PHP, $_ POST ['title'] can get the value of title, and $_ POST [' sub'] can get the sub array.

Most of the time, we also use this method when we submit data using Ajax. For example, the default value for Ajax and Content-Type for both Jquery and QWrap is "application/x-www-form-urlencoded;charset=utf-8".

Multipart/form-data

This type of POST is also common. When we use the form to upload files, we have to make the enctyped of form equal to this value. Here is an example:

POST http://www.example.com HTTP/1.1

Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA

-WebKitFormBoundaryrGKCBY7qhFd3TrwA

Content-Disposition: form-data; name= "text"

Title

-WebKitFormBoundaryrGKCBY7qhFd3TrwA

Content-Disposition: form-data; name= "file"; filename= "chrome.png"

Content-Type: image/png

PNG... Content of chrome.png...

-WebKitFormBoundaryrGKCBY7qhFd3TrwA--

This example is a little more complicated. First, a boundary is generated to split different fields, and the boundary is long and complex to avoid repetition with the body content. Then, the Content-Type indicates that the data is encoded in mutipart/form-data, and what is the content of the boundary of this request. According to the number of fields, the message body is divided into several parts with similar structure. Each part starts with-- boundary, followed by content description information, then enter, and finally the specific content of the field (text or binary). If you are transferring a file, also include the file name and file type information. The message body ends with-- boundary--. For a detailed definition of mutipart/form-data, please go to rfc1867.

This method is generally used to upload files, and the major server languages also have good support for it.

The above two POST data methods are natively supported by browsers, and native form forms only support these two ways at this stage. But as more and more Web sites, especially WebApp, all use Ajax for data interaction, we can define new ways to submit data, bringing more convenience to development.

Application/json

You must be familiar with the Content-Type of application/json as a response header. Now more and more people use it as a request header to tell the server that the message body is a serialized JSON string. Due to the popularity of the JSON specification, all major browsers except the lower version of IE natively support JSON.stringify, and server languages also have functions to deal with JSON, so there will be no trouble using JSON.

It is useful that the JSON format supports structured data that is much more complex than key-value pairs. Remember, when I was working on a project a few years ago, the data I needed to submit was very deep. I serialized the data JSON and then submitted it. However, at that time, I used the JSON string as a val, which was still placed in the key-value pair and submitted as x-www-form-urlencoded.

The default Ajax function in Google's AngularJS is to submit JSON strings. For example, the following code:

Var data = {'title':'test',' sub': [1je 2jue 3]}; $http.post (url, data) .success (function (result) {...})

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 scheme can easily submit complex structured data and is especially suitable for the interface of RESTful. All the major package grabbing tools, such as Chrome's own developer tools, Firebug, Fiddler, will display JSON data in a tree structure, which is very friendly. However, there are some server-side languages that do not support this approach. For example, php cannot get content from the above request through the $_ POST object. At this point, you need to do it yourself: when Content-Type is application/json in the request header, get the original input stream from php://input, and then json_decode it into an object. Some php frameworks are already starting to do this.

Of course, AngularJS can also be configured to submit data using x-www-form-urlencoded.

Text/xml

XML-RPC (XML Remote Procedure Call is a remote invocation specification that uses HTTP as the transport protocol and XML as the encoding. A typical XML-RPC request looks like this:

POST http://www.example.com HTTP/1.1Content-Type: text/xmlexamples.getStateName41

XML-RPC is simple and functional, and it can be implemented in all kinds of languages. It is also widely used, such as XML-RPC Api of WordPress, ping service of search engine and so on. In JavaScript, there are also ready-made libraries to support data exchange in this way, which can well support existing XML-RPC services. However, I personally think that the XML structure is still too bloated, and it is more flexible and convenient to use JSON in general scenarios.

At this point, the study on "what are the common ways for POST to submit data" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report