Post request transmission interface

I. Introduction

  All systems or software, log on the site are from the start, so the first post login request is first introduced.

Two, help function

  Shortcut to learning a new module, direct () function to see the relevant comments and content with the help Case

   for example:

import requests
help(requests)

Third, the post request transmission interface (dict parameter)

  Case 1. Interface post transmission request provided by python, slightly modified to be a simple, can send a simple post request

  2. Like the case of the official documents given payload type parameter is a dictionary (dict), spread to form in the following figure

import requests
url = "https://httpbin.org/post"
payload = {"username":"[email protected]","password":"666"}
r = requests.post(url=url,data=payload)
print(r.text)

Fourth, the interface sends a post request (JSON parameter)

  1.post the body is json type, you can also pass parameters with json

  2. The first import json module, the method used to transform into json format dumps

  3. Return result, spread data in

import requests
import json
url = "https://httpbin.org/post"
payload = {"username":"[email protected]","password":"666"}
data_json = json.dumps(payload)  # 转换成json格式
r = requests.post(url=url,json=data_json)
print(r.text)

Fifth, the header request header

  Now people pay attention to security interface, making more and more complex analog Login

import requests
url = "https://httpbin.org/post"
headers = {"connection" :"keep-alive","host":"httpbin.org","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}
r = requests.post(url=url,headers=headers)
print(r.json())

 PS:切记,脚本里的关键字headers不要写成header!!!

Guess you like

Origin www.cnblogs.com/huainanhai/p/12014328.html