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

10.1k Views Asked by At

I am trying to trigger airflow dags with REST API. It does not work. I get an ERROR 400 with response:

{
  "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"
}

I tried it via CURL and with Python requests module and results are the same.

Example:

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))

Do I need to specify something in Airflow config or in Dag to make it possible to run it? Because as I understand there is something with permissions ? "Property is read-only - 'state'",

2

There are 2 best solutions below

4
On BEST ANSWER

Try removing the state key from the body.

i.e.

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

The Airflow REST API docs for that endpoint says state is required in the body, however you shouldn't need to include that in your request. I've tested it locally (Airflow v2.0.1) without state in the body of the request and it appears to work!

2
On

Could solve it, with:

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)
                 )

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