python - 如何使用 python 中的请求和时间模块向 API 端点发送随机获取请求
问题描述
如何使用 python 中的请求和时间模块向 API 端点发送随机获取请求
解决方案
如果您只想要一个如何执行请求的示例,这里是一个从 api 获取产品列表的请求。
# import the Requests library
import requests
# define the base URL for the API
base_url = 'http://127.0.0.1:8000/api/'
# random example of a get a list of products,(put your specific endpoint)
r = requests.get(f'{base_url}products/')
# json() method of the response object to decode the JSON data returned by the API
products = r.json()
products_list = ', '.join([product['name'] for product in products])
# print products names
print(f'Available products: {products_list}')
或者您可以在函数或类中使用“产品”作为上下文
推荐阅读
- html - 如何移动右侧表格边距旁边的滚动条
- android - 使用 MediaCodec 和 MediaMuxer 将 PCM 编码为 AAC 时没有音频
- azure-ad-b2c - 在 Azure Ad B2C 中自助密码重置需要进行 2 次电子邮件验证
- java - 无法在索引中查看 ElasticSearch 创建的文档
- swift - HackerEarth 中的 Unlucky 13 挑战
- node.js - 如何修复此错误:找不到模块“moz”Nodejs
- c# - WPF 如何在数据网格中使用行标题更改列的标题样式?
- maas - MAAS 显示机器的许多核心
- apple-appclips - 为什么 App Clip Code 有效载荷大小限制如此之小
- haskell - Data.Vector.Storable.ZipWith 中的慢流融合,但 Data.Vector.Unboxed.ZipWith 中的快速流融合