Detailed explanation of the difference between GET/POST request (interface test actual combat)

image

In daily work, the two most commonly used HTTP requests are GET and POST. A deep understanding of the principles, similarities and differences between these two request methods is also an important foundation for subsequent interface testing.

Summary of the difference between GET and POST

  1. The method of the request line is different;

  2. POST can attach body, and can support various data formats such as form, json, xml, binary, etc.;

  3. From the perspective of general industry specifications, it is recommended to use GET requests for stateless changes, and POST requests for data writing and status;

Demo environment setup

In order to avoid interference from other factors, use Flask to write a simple Demo Server.

  1. Install flask

pip install flask
  1. Create a hello.py file

hello.py

from flask import Flask, request

app = Flask (_name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route("/request", methods=['POST', 'GET']) 
def hellp():
    #拿到request参数 
    query = request.args 
    #El request form 
    post = request.form 
    #分别打印拿到的参数和form 
    return f"query: {query}\n"\
           f"post: {post}"
  1. Start service

export FLASK_APP=hello.py 
flask run

If the following information is prompted, the setup is successful.

* Serving Flask app "hello.py" 
* Environment: production 
  WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. 
* Debug mode: off 
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

CURL command initiates GET/POST request

Initiate a GET request, put the a and b parameters in the URL to send, and save in the get file:

curl 'http://127.0.0.1:5000/request?a=1&b=2' -V -S &>get

Initiate a POST request, and the a and b parameters are sent in form-data format and saved in the post file:

curl 'http://127.0.0.1:5000/request?' -d "a=1&b=2" -V -S &>post

GET/POST request comparison

Note: >The right side is the request content, and the <left side is the response content.

GET request process

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET /request?a=1&b=2 HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.64.1
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 80
< Server: Werkzeug/0.14.1 Python/3.7.5
< Date: Wed, 01 Apr 2020 07:35:42 GMT
<
{ [80 bytes data]
* Closing connection 0
query: ImmutableMultiDict([('a', '1'), ('b', '2')])
post: ImmutableMultiDict([])

POST request process

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /request?a=1&b=2 HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Length: 7
> Content-Type: application/x-www-form-urlencoded
>
} [7 bytes data]
* upload completely sent off: 7 out of 7 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 102
< Server: Werkzeug/0.14.1 Python/3.7.5
< Date: Wed, 01 Apr 2020 08:15:08 GMT
<
{ [102 bytes data]
* Closing connection 0
query: ImmutableMultiDict([('a', '1'), ('b', '2')])
post: ImmutableMultiDict([('c', '3'), ('d', '4')])

Compare the two files:

image

It can be clearly seen from the figure that the method of the GET request is GET, and the method of the POST request is POST. In addition, the GET request does not have Content-Type and Content-Length fields, and the URL in the request line has the query parameter. Formats allowed for both requests. (End)

The following is my collection and sorting in recent years, the whole is organized around [software testing], the main content includes: python automation test exclusive video, Python automation details, a full set of interview questions and other knowledge content.
Insert picture description hereInsert picture description here
For software testing friends, it should be the most comprehensive and complete interview preparation warehouse. In order to better organize each module, I also refer to many high-quality blog posts and projects on the Internet, and strive not to miss every knowledge point. Friends relied on these contents to review and got offers from big factories such as BATJ. This warehouse has also helped many learners of software testing, and I hope to help you too.

Follow the WeChat public account [Programmer Erhei] to receive Python automated testing super hard core resources

Guess you like

Origin blog.csdn.net/m0_52650621/article/details/112787997