For interface automation testing, you must master these four methods of post submission data.

We all know that POST is generally used to submit data to the server. The four formats of POST submitted data are the four forms of Content-Type. Pay special attention to the format of the data in the body when http sends a request in each format. The 4 forms are:

1. application/x-www-form-urlencoded: URL encoded.

2. multipart/form-data: key-value pair data.

3. application/json: Json type data.

4. text/xml: xml.

The HTTP request methods specified by the HTTP/1.1 protocol include OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, and CONNECT. POST is usually used to submit data to the server, mainly used to submit forms and upload files.

The HTTP protocol is transmitted in ASCII code and is an application layer specification based on the TCP/IP protocol. The specification divides HTTP requests into four parts: request line, request header, blank line, and request body. Something like this:

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

But when the data is sent out, it only makes sense if the server parses it successfully. General server-side languages such as PHP, Python, etc., and their frameworks have built-in functions for automatically parsing common data formats. The server usually learns how the message body in the request is encoded based on the Content-Type field in the request headers, and then parses the body. So when it comes to the POST submission data scheme, it includes two parts: Content-Type and message body encoding method. We will officially introduce them below.

application/x-www-form-urlencoded

Introduction

This is the most common way to submit data via POST. If the browser’s native

form does not set the enctype attribute, the data will eventually be submitted in application/x-www-form-urlencoded mode. The request is similar to the following (irrelevant request headers are omitted in this article):

First, the Content-Type is specified as application/x-www-form-urlencoded; secondly, the submitted data is encoded according to key1=val1 & key2=val2, and both key and val are URL transcoded. Most server-side languages have good support for this method.

Many times, we also use this method when submitting data using Ajax. For example, in JQuery’s Ajax, the default Content-Type value is application/x-www-form-urlencoded;charset=utf-8.

Examples

Write Python code:

requests determines which method to use based on the incoming key. The above method uses urlencode to encode the data. Here, data= is passed in parameters, and the parameter format is a Python dict dictionary.

The printout is as follows:

? Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 485187702 [password: csdn11]

multipart/form-data

Introduction

This encoding method is usually used when the client transmits large file data, such as pictures or files, to the server. It is a common POST data submission method. When we use a form to upload files, we must make the enctype of the

form equal to multipart/form-data. This format is used to upload files. Let’s look directly at a request example:

First, a boundary string will be generated, indicating that the following is the form content, followed by the name of the first key-value pair in the form, followed by a newline, followed by the value. Then generate a boundary string dividing line to separate different key values. If a file is being transferred, the file name and file type information must also be included. This method is generally used to upload files, and major server languages also have good support for it.

The two POST data methods mentioned above are natively supported by browsers, and the native

form in the current standard only supports these two methods (specified through the enctype attribute of the

element, the default is application/x-www-form-urlencoded. In fact, enctype also supports text/plain, but it is rarely used).

As more and more Web sites, especially WebApps, all use Ajax for data interaction, we can completely define new data submission methods.

Examples

Write Python code:

It’s also data=passing parameters, but the difference is that the data is encoded in a different way.

The printout is as follows:

application/json

Introduction

Application/json is definitely familiar to everyone as a response header. It is used a lot and is very convenient. Setting the Content-type in the header tells the server that the data exists in the form of a Json string. Correspondingly, just use the Json method to decode the data. In fact, more and more people now 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 low-level IE natively support JSON.stringify. Server-side languages also have functions for processing Json, so you will not encounter any trouble when using Json.

The Json format supports structured data that is much more complex than key-value pairs. The Ajax function in Google’s AngularJS submits a Json string by default. For example, the following code:

The final request sent is

This solution can easily submit complex structured data, and is especially suitable for RESTful interfaces. Major packet capture tools, such as Chrome’s own developer tools, Firebug, and Fiddler, will display Json data in a tree structure, which is very friendly.

Example

Write Python code:

Use json= to pass in parameters, the format of which is Json string, so you need to use json.dumps() to convert Python dict to Json string (actually it is Python’s str type, but the receiver will decode the string into Json).

The printout is as follows:

text/xml

Introduction

It is a remote call specification that uses HTTP as the transport protocol and XML as the encoding method. A typical XML-RPC request looks like this:

The XML-RPC protocol is simple, functional and available in various languages. It is also widely used, such as WordPress’s XML-RPC Api, search engine’s ping service, etc. In JavaScript, there are also ready-made libraries that support data interaction in this way, and can well support existing XML-RPC services. However, I personally feel that the XML structure is still too bloated, and it is more flexible and convenient to use Json in general scenarios.

Example

Write Python code:

Use data= to pass parameters. The parameter type is a string and must be written according to xml syntax.

The printout is as follows:

Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly! Hope this helps! [100% free without any tricks]