url = 'http://example.com/api/endpoint'data = {'key1': 'value1', 'key2': 'value2'}response = requests.post(url, data=data)if response.status_code == 200: print('API request successful') print(response.json())els
url = "https://example.com/login"data = { "username": "your_username","password": "your_password"} try:response = requests.post(url, data = data)response.raise_for_status() # 检查状态码,如果不是200系列,会抛出异常 print("请求成功")print(response.text)except requests.RequestException ...
response import Response from rest_framework import status @api_view(['POST']) def process_post_data(request): received_data = request.data # 处理接收到的数据 return Response({'message': 'Data received successfully', 'data': received_data}, status=status.HTTP_200_OK) 在另一个 Python 脚本...
response = requests.post('https://api.example.com/post', data=data)处理响应:处理API响应,可以获取响应的状态码、头部信息和响应内容等。if response.status_code == 200:print("Request was successful!")print("Response JSON:", response.json())else:print("Request failed with status code:", respo...
1<method><request-URL><version><headers><entity-body> 协议规定 POST 提交的数据必须放在消息主体(entity-body)中,但协议并没有规定数据必须使用什么编码方式。实际上,开发者完全可以自己决定消息主体的格式,只要最后发送的 HTTP 请求满足上面的格式就可以。
在 requests 库中,通过 post 方法可以轻松发送 POST 请求,并且可以使用 json 参数直接传递 JSON 数据:response = requests.post(url, json=data)4. 处理响应 一般来说,服务器会返回一个响应对象。你可以通过该对象访问响应的状态码、响应体等信息:if response.status_code == 200: print('Request was s...
值得注意的是,如果API需要使用POST方法,请求数据应该放在Request体中,并且在请求头中设置Content-Type为application/json。示例代码如下: import requests url = "https://api.example.com" data = {'key': 'value'} headers = {'Content-Type': 'application/json'} ...
import requests# 创建会话session = requests.Session()# 第一个请求response1 = session.get('https://api.example.com/login')# 第二个请求response2 = session.post('https://api.example.com/data', data={'key': 'value'})# 输出响应内容print(response2.text)在上述代码中,我们使用requests.Session...
url = "https://example.com/api" data = {"key1": "value1", "key2": "value2"} try: response = requests.post(url, data=data) response.rAIse_for_status() # 检查响应是否为200 print(response.text) except requests.exceptions.RequestException as e: ...
get('http://example.com/api') 此外还可以传入其他参数,如headers, params等。 2. 获取响应 发送请求后可以获取Server响应信息: resp.status_code: 响应状态码 resp.headers: 响应头部信息 resp.text/content: 响应内容,text为字符串,content为字节 resp.json():将JSON响应解析为字典或列表 示例: 代码语言:...