如何使用 REST API 触发气流 dag(我得到 "Property is read-only - 'state'",错误)

How to trigger airflow dag with REST API (I get "Property is read-only - 'state'", error)

我正在尝试使用 REST API 触发气流 dags。这是行不通的。我收到错误 400 响应:

{
  "detail": "Property is read-only - 'state'",
  "status": 400,
  "title": "Bad Request",
  "type": "https://airflow.apache.org/docs/2.0.1/stable-rest-api-ref.html#section/Errors/BadRequest"
}

我通过 CURL 和 Python 请求模块进行了尝试,结果是一样的。

示例:

import requests

headers = {
    'accept': 'application/json',
    'Content-Type': 'application/json',
}
auth = ('test', 'test')
import json
body = {
  "conf": {},
  "dag_run_id": "string",
  "execution_date": "2021-04-15T14:04:43.602Z",
  "state": "success"
}
req = requests.post("http://127.0.0.1:8080/api/v1/dags/sleeper/dagRuns",
                   headers=headers, auth=auth, data=json.dumps(body))

我是否需要在 Airflow 配置或 Dag 中指定一些内容才能 运行 它? 因为据我所知,有些东西有权限? "Property is read-only - 'state'",

尝试从正文中删除 state 键。

body = {
  "conf": {},
  "dag_run_id": "string",
  "execution_date": "2021-04-15T14:04:43.602Z"
}

Airflow REST API docs for that endpoint 表示正文中需要 state,但是您不需要在请求中包含它。我已经在本地 (Airflow v2.0.1) 测试了它,请求正文中没有 state,它似乎可以工作!

可以解决它,用:

import requests
import json

headers = {
    'accept': 'application/json',
    'Content-Type': 'application/json',
}

auth = ('test', 'test')

body = {
  "conf": {},
}

r = requests.post("http://127.0.0.1:8080/api/v1/dags/sleeper/dagRuns",
                   headers=headers, 
                   auth=auth, 
                   data=json.dumps(body)
                 )

参见link:http://localhost:8080/api/v1/ui/#/DAGRun/post_dag_run