Read this article to understand what are the get and post requests of the Python requests module?

1 Introduction

(Solemnly declare: The copyright of this blog post belongs to Sweeping Monk-smile , and reprinting of the blog post is prohibited!)

(Follow the blogger, update the blog from time to time, every article is a boutique, full of dry goods!!!)

​Sweeping Monk-smile devotes himself to building a nanny-level knowledge point blog, from raising questions to comprehensive solutions, just reading this article is enough. This blog brings together the following advantages .

  • Complete knowledge about the problem

  • Logical problem solving

  • All demo codes are available : no garbled characters, clear comments, reproducible, all codes are self-developed, and uploaded after the test is correct.

2 GET requests

*Note: Each section is divided into two parts. The content above is the python requestsrequest , and the content below is the original request message obtainedsocket by packet capture .http

  • url

    requests.get(url = "http://127.0.0.1:6000/")
    
    GET / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    
  • with parameters

    requests.get(url = "http://127.0.0.1:6000/?name=smile")
    
    GET /?name=smile HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    
  • with parameters

    params = {
          
          
        "name": "smile"
    }
    
    requests.get(url="http://127.0.0.1:6000/", params = params)
    
    GET /?name=smile HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    
  • send json

    json_ = {
          
          
        "name": "smile"
    }
    
    requests.get(url="http://127.0.0.1:6000/", json = json_ )
    
    GET / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 17
    Content-Type: application/json
    
    {"name": "smile"}
    
  • send data

    data = {
          
          
        "name": "smile"
    }
    
    requests.get(url="http://127.0.0.1:6000/", data = data)
    
    GET / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 10
    Content-Type: application/x-www-form-urlencoded
    
    name=smile
    

3 POST requests

*Note: Each section is divided into two parts. The content above is the python requestsrequest , and the content below is the original request message obtainedsocket by packet capture .http

  • url

    requests.post(url = "http://127.0.0.1:6000/")
    
    POST / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 0
    
  • with parameters

    requests.post(url = "http://127.0.0.1:6000/?name=smile")
    
    POST /?name=smile HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 0
    
  • with parameters

    params = {
          
          
        "name": "smile"
    }
    
    requests.post(url="http://127.0.0.1:6000/", params = params)
    
    POST /?name=smile HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 0
    
  • send json

    json_ = {
          
          
        "name": "smile"
    }
    
    requests.post(url="http://127.0.0.1:6000/", json_ = data)
    
    POST / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 17
    Content-Type: application/json
    
    {"name": "smile"}
    
  • send data

    data = {
          
          
        "name": "smile",
        "age": "26"
    }
    
    requests.post(url="http://127.0.0.1:6000/", data = data)
    
    POST / HTTP/1.1
    Host: 127.0.0.1:6000
    User-Agent: python-requests/2.27.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 10
    Content-Type: application/x-www-form-urlencoded
    
    name=smile&age=26
    

4 Thank you for watching

Guess you like

Origin blog.csdn.net/z132533/article/details/125952307