Python - AI 助手

Python Requests post() 方法



Python Requests 的 post() 方法用於向指定的 URL 傳送 HTTP POST 請求。它允許向伺服器傳送資料,通常用於提交表單或上傳檔案。

此方法接受諸如“url”、“data”、“json”、“headers”、“cookies”、“files”和“timeout”之類的引數。使用“data”或“json”分別傳送表單資料或 JSON 有效負載,其中“headers”可用於傳送自定義 HTTP 標頭,“files”可用於上傳檔案。

它返回一個響應物件,其中包含狀態程式碼、標頭和內容等詳細資訊,從而促進與 Web 服務的互動。

語法

以下是 Python Requests post() 方法的語法和引數:

requests.post(url, data=None, json=None, headers=None, params=None, auth=None, timeout=None, verify=None)

引數

以下是 Python Requests post() 方法的引數:

  • url: 傳送請求的 URL。
  • data: 請求的主體。這可以是字典、位元組或類檔案物件,用於傳送到請求的主體中。
  • json: 傳送到請求主體中的 JSON 資料。
  • headers: 與請求一起傳送的 HTTP 標頭。
  • files: 與請求一起上傳的檔案。
  • auth: Auth 元組以啟用 Basic/Digest/Custom HTTP Auth 或自定義身份驗證可呼叫物件。
  • cookies: 與請求一起傳送的 Cookie。
  • timeout: 請求的超時時間。
  • allow_redirects: 確定是否應跟蹤重定向。
  • proxies: 一個字典,將協議對映到代理的 URL。
  • verify: 控制是否啟用 SSL 證書驗證。

返回值

此方法返回一個 Response 物件。

示例 1

以下是一個基本示例,它使用 python requests 的 post() 方法向指定的 URL 傳送一個簡單的 POST 請求,並使用表單資料:

import requests

# Define the URL
url = 'https://httpbin.org/post'

# Define the data to be sent in the request body
data = {'key1': 'value1', 'key2': 'value2'}

# Send the POST request with the data
response = requests.post(url, data=data)

# Print the response status code
print('Status Code:', response.status_code)

# Print the response content
print('Response Content:', response.text)

輸出

Status Code: 200
Response Content: {
  "args": {},
  -------------
  ------------
  ------------
"json": null,
"origin": "110.226.149.205",
"url": "https://httpbin.org/post"
}

示例 2

伺服器將接收帶有提供的 JSON 資料的 POST 請求,並列印來自伺服器的響應,包括狀態程式碼和任何響應內容。這是一個示例:

import requests

# Define the URL
url = 'https://httpbin.org/post'

# Define the JSON data to be sent in the request body
json_data = {'key1': 'value1', 'key2': 'value2'}

# Send the POST request with the JSON data
response = requests.post(url, json=json_data)

# Print the response status code
print('Status Code:', response.status_code)

# Print the response content
print('Response Content:', response.text)

輸出

Status Code: 200
Response Content: {
  "args": {},
  "data": "{\"key1\": \"value1\", \"key2\": \"value2\"}",
  -------------
  ------------
  ------------
"json": {
    "key1": "value1",
    "key2": "value2"
  },
  "origin": "110.226.149.205",
  "url": "https://httpbin.org/post"
}

示例 3

以下是一個使用 Python 的 requests 模組 post() 方法傳送帶有超時設定的 POST 請求的示例:

import requests

# Define the URL
url = 'https://httpbin.org/post'

# Define the JSON data to be sent in the request body
json_data = {'key1': 'value1', 'key2': 'value2'}

# Set the timeout for the request (in seconds)
timeout = 5

try:
    # Send the POST request with the JSON data and timeout
    response = requests.post(url, json=json_data, timeout=timeout)

    # Print the response status code
    print('Status Code:', response.status_code)

    # Print the response content
    print('Response Content:', response.text)

except requests.Timeout:
    # Handle timeout error
    print('Timeout Error: Request timed out.')

except requests.RequestException as e:
    # Handle other request exceptions
    print('Request Exception:', e)

輸出

Status Code: 200
Response Content: {
  "args": {},
  "data": "{\"key1\": \"value1\", \"key2\": \"value2\"}",
  -------------
  ------------
  ------------
 },
  "origin": "110.226.149.205",
  "url": "https://httpbin.org/post"
}
python_modules.htm
廣告