前言

首先确保安装了requests模块,如果没有安装,则使用下面的命令安装:

pip install requests

1.POST请求(携带参数)

form请求:

import requests,json

url = "http://localhost:8091/user/test"

data = {
"phone": "13888888888",
"smscode": "11516",
}

headers = {'Content-type': 'application/x-www-form-urlencoded'}
# headers = {'Content-type': 'application/json'}

response = requests.post(url, data, headers=headers)

print(response.content.decode())

json请求:

注意!发送json请求时,需要使用json.dumps()方法将参数编码,才能成功发送json请求。

import requests,json

url = "http://localhost:8091/user/login"

data = {
"phone": "13888888888",
"smscode": "11516",
}

# headers = {'Content-type': 'application/x-www-form-urlencoded'}
headers = {'Content-type': 'application/json'}

response = requests.post(url, json.dumps(data), headers=headers)

print(response.content.decode())

 

2.GET请求

form请求:

import requests,json

url = "http://localhost:8091/user/test"

data = {
"phone": "13888888888",
"smscode": "11516",
}

headers = {'Content-type': 'application/x-www-form-urlencoded'}
# headers = {'Content-type': 'application/json'}

response = requests.get(url, data, headers=headers)

print(response.content.decode())

 

json请求:

import requests,json

url = "http://localhost:8091/user/login"

data = {
"phone": "13888888888",
"smscode": "11516",
}

# headers = {'Content-type': 'application/x-www-form-urlencoded'}
headers = {'Content-type': 'application/json'}

response = requests.get(url, data, headers=headers)

print(response.content.decode())