用Python給服務器發Post請求的簡明例子

用Python給server發post request可以用強大的request庫。以下是例子,幾行就寫完。包括import library,construct request,發送post請求,檢查response,根據是否成功輸出不同的信息。

import requests

url = 'http://your-server.com/api/endpoint'
data = {'key': 'value'}

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

if response.status_code == 200:
    print('Success!')
else:
    print('Failed with status code:', response.status_code)

Leave a Comment

Your email address will not be published.