Introduction to the

After the introduction of the interface to send get requests, we must think of the interface to send POST requests is not too difficult, by the smart you guessed again. The answer is correct, although the reference example of sending a POST request is simple, the actual situation encountered is more complicated because all systems or software or websites start from login

So the first POST request is definitely a login request, but the login is the hardest one to process. If the login problem is solved, then the rest of the problems will be solved.

Help function

The requests module is for you to visit the official website and view the official documents. In fact, learning a new module shortcut, do not go to Baidu what, directly use the help function can view the relevant notes and case content.

Python interface for sending GET requests:

Python post request interface example:

Interface to send post requests (dict arguments)

In the last article, we saw the power and power of Requests. Get and POST requests can be implemented in a few lines of code. Here must be everyone can’t wait to introduce, good, then we come to a small try.

1. Use the python POST request interface example given above, with a few minor modifications, to send a simple POST request

The payload is a dict parameter, and you can send it to the form shown in the following figure

Interface to send post requests (JSON parameters)

1. The body of the post is a JSON type, which can also be passed in as a JSON parameter.

2. Import json module and convert it to JSON format with dumps method.

3. Return the result and upload it to data

Request header header

Nowadays, the requirements for interface security make simulated login more and more complex than the basic content described above. Generally speaking, as long as the landing involves security considerations, then the landing will be more complicated.

1, take blog park as an example, a few years ago simulated login, not involving security considerations relatively simple. Now its login involves security considerations, so the actual situation is much more complicated than the above mentioned.

2. You can also add the headers to a request for data (passed by the HEADERS keyword). Sometimes, you must add the headers to a special request to return the result. For example, if you add headers to a blog login, it does not mean that a blog login must be logged in to return

Instead, it is used as an example of adding the request header parameter to the login request interface. Here you can use the packet capture tool to see the request header at login. The following is the request header for logging in to the Fiddler tool during packet capture.

3, as the way of logging in blog park has changed, let’s change an example to demonstrate. Or to the official document to demonstrate the site below

(1) Capture the packet, check its request header, and check the browser F12

Fiddler captures the packet and checks the request header. The result is as follows:

Example code and its result:

Report errors and solutions when encountering problems:

raise SSLError(e, request=request)

requests.exceptions.SSLError: HTTPSConnectionPool(host=’httpbin.org’, port=443): Max retries exceeded with url: /post (Caused by SSLError(SSLError(“bad handshake: Error([(‘SSL routines’, ‘tls_process_server_certificate’, ‘certificate verify failed’)])”)))

summary

SSLError: HTTPSConnectionPool(host=’httpbin.org’, port=443): Max retries exceeded with URL: /post (Caused by SSLError(SSLError(“bad handshake: Error([(‘SSL routines’, ‘tls_process_server_certificate’, ‘certificate verify failed’)])”)))

Verify =False: verify=False: verify=False

InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: Urllib3. Readthedocs. IO/en/latest/a… InsecureRequestWarning)

The payload is in JSON format. The payload is transmitted as a JSON parameter. Write the request header in dictionary format and pass the parameter.

4, the final result is in JSON format, can be directly used to return JSON data:

{‘args’: {}, ‘data’: ”, ‘files’: {}, ‘form’: {}, ‘headers’: {‘Accept’: ‘*/*’, ‘Accept-Encoding’: ‘gzip, deflate’, ‘Content-Length’: ‘0’, ‘Host’: ‘httpbin.org’, ‘user-agent ‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64; Trident / 7.0; Rv :11.0) like Gecko’}, ‘json’: None, ‘Origin ‘: ‘222.128.10.95, 222.128.10.95’, ‘url’: ‘httpbin.org/post’}

Json parameters are very similar to dict parameters. They are both key-values.

In order to facilitate you to see my blog posts on the mobile terminal, I have registered my personal wechat public account and can scan the QR code at the bottom left. You are welcome to pay attention to it. I will share relevant technical blog posts in time.

In order to facilitate the interaction and discussion of relevant technical issues, a special wechat group has been set up. Since the number of wechat groups is 100, please scan the QR code of Hongge’s personal wechat to get you into the group

(Please be sure to note: into the group)

Welcome to join this big family. Let’s swim the ocean of knowledge together.

Thank you for taking the time to read this article. If you think you have learned something from this article, it is also for rewarding the blogger for having a cup of coffee. Thank you! If you find reading this article helpful, please click the “Recommend” button in the lower left corner of your

“Recommended”

Will be my biggest writing motivation! In addition, you can also choose [focus on me], it is very convenient to find me! The copyright of this article belongs to the author and the blog park, the source website: www.cnblogs.com/du-hong welcome to reprint, but without the consent of the author, after reprint the article must be in the obvious position of the article page to give the author and the original link, otherwise reserve the right to pursue legal responsibility!