Python

Integrate Strapi with Python.

Python 入门

本集成指南遵循 快速入门指南,并假设您已完全完成“动手”路径。您应该能够通过浏览 URL http://localhost:1337/api/restaurants 来使用该 API。

如果您尚未阅读快速入门指南,则使用 Python 请求 Strapi API 的方式保持不变,只是您不会获取相同的内容。

创建 Python 文件

请确保您的计算机上已安装 Python

bash
touch script.py

使用 HTTP 客户端

有许多 HTTP 客户端可用,以下示例使用 Requests。要安装 Requests,请在终端中运行以下命令:

bash
python -m pip install request

向您的集合类型发送 GET 请求

restaurant 集合类型执行 GET 请求以获取所有餐厅。

确保您已为 restaurant 集合类型激活 find 权限。

GET 请求示例
python
requests.get("http://localhost:1337/api/restaurants")
json
{
  "data": [
    {
      "id": 1,
      "attributes": {
        "name": "Biscotte Restaurant",
        "description": "Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers.",
        "createdAt": "2022-07-31T11:57:01.330Z",
        "updatedAt": "2022-07-31T11:57:44.945Z",
        "publishedAt": "2022-07-31T11:57:44.943Z"
      }
    }
  ],
  "meta": {
    "pagination": {
      "page": 1,
      "pageSize": 25,
      "pageCount": 1,
      "total": 1
    }
  }
}

Example

python
import json
import requests

class Restaurant:
    def __init__(self):
        self.api_url = "http://localhost:1337/api"

    def all(self):
        r = requests.get(self.api_url + "/restaurants")
        return r.json()

restaurant = Restaurant()
print(restaurant.all())

POST 请求您的集合类型

对“restaurant”集合类型执行“POST”请求以创建餐厅。

确保您已激活“restaurant”集合类型的“create”权限和“category”集合类型的“find”权限。

添加“?populate=categories”查询参数以返回带有响应的类别。

在此示例中,已创建具有 id:3 的“japanese”类别。

POST 请求示例
python
requests.post(
    "http://localhost:1337/api/restaurants",
    headers={"Content-Type": "application/json"},
    params={"populate": "categories"},
    data=json.dumps(
        {
            "data": {
                "name": "Dolemon Sushi",
                "description": "Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious",
                "categories": [3],
            }
        }
    ),
)
json
{
  "data": {
    "id": 2,
    "attributes": {
      "name": "Dolemon Sushi",
      "description": "Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious",
      "createdAt": "2022-07-31T15:36:56.336Z",
      "updatedAt": "2022-07-31T15:36:56.336Z",
      "publishedAt": "2022-07-31T15:36:56.336Z",
      "categories": {
        "data": [
          {
            "id": 3,
            "attributes": {
              "name": "japanese",
              "createdAt": "2022-07-31T11:57:35.040Z",
              "updatedAt": "2022-07-31T11:57:35.631Z",
              "publishedAt": "2022-07-31T11:57:35.629Z"
            }
          }
        ]
      }
    }
  },
  "meta": {}
}

Example

python
import json
import requests

class Restaurant:
    def __init__(self):
        self.api_url = "http://localhost:1337/api"

    def all(self):
        r = requests.get(self.api_url + "/restaurants")
        return r.json()

    def create(self, params):
        r = requests.post(
            self.api_url + "/restaurants",
            headers={"Content-Type": "application/json"},
            data=json.dumps(
                {
                    "data": {
                        "name": params["name"],
                        "description": params["description"],
                        "categories": params["categories"],
                    }
                }
            ),
        )
        return r.json()

restaurant = Restaurant()
print(
    restaurant.create(
        {
            "name": "Dolemon Sushi",
            "description": "Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious",
            "categories": [3],
        }
    )
)

PUT 请求您的集合类型

对“餐厅”集合类型执行“PUT”请求,以更新餐厅的类别。

确保您已为“餐厅”集合类型激活“put”权限。

PUT 请求示例
python
requests.put(
    "http://localhost:1337/api/restaurants/2",
    params={"populate": "categories"},
    headers={"Content-Type": "application/json"},
    data=json.dumps(
        {
            "data": {
                "categories": [2],
            }
        }
    ),
)
json
{
  "data": {
    "id": 2,
    "attributes": {
      "name": "Dolemon Sushi",
      "description": null,
      "createdAt": "2022-07-31T15:43:44.039Z",
      "updatedAt": "2022-07-31T15:46:29.903Z",
      "publishedAt": "2022-07-31T15:43:44.038Z",
      "categories": {
        "data": [
          {
            "id": 2,
            "attributes": {
              "name": "Brunch",
              "createdAt": "2022-07-31T11:57:23.472Z",
              "updatedAt": "2022-07-31T11:57:25.180Z",
              "publishedAt": "2022-07-31T11:57:25.179Z"
            }
          }
        ]
      }
    }
  },
  "meta": {}
}

Example

python
import json
import requests

class Restaurant:
    def __init__(self):
        self.api_url = "http://localhost:1337/api"

    def all(self):
        r = requests.get(self.api_url + "/restaurants")
        return r.json()

    def create(self, params):
        r = requests.post(
            self.api_url + "/restaurants",
            headers={"Content-Type": "application/json"},
            data=json.dumps(
                {
                    "data": {
                        "name": params["name"],
                        "description": params["description"],
                        "categories": params["categories"],
                    }
                }
            ),
        )
        return r.json()

    def update(self, id, params):
        r = requests.put(
            self.api_url + "/restaurants/" + str(id),
            headers={"Content-Type": "application/json"},
            params={"populate": "categories"},
            data=json.dumps({"data": {"categories": params["categories"]}}),
        )
        return r.json()

restaurant = Restaurant()
print(restaurant.update(2, {"categories": [2]}))