4

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'",

Jakub Pluta
  • 147
  • 5
  • 13

2 Answers2

5

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!

Josh
  • 1,556
  • 1
  • 10
  • 21
  • Hi, that was a case :) But anyway when I am trying to execute script via web http://127.0.0.1:8080/api/v1/ui/#/DAGRun/post_dag_run it's working, but localy when I try to execute it with my python code I get ```{ "detail": null, "status": 403, "title": "Forbidden", "type": "https://airflow.apache.org/docs/2.0.1/stable-rest-api-ref.html#section/Errors/PermissionDenied" }```. Do you know if I need add something in my python script to have permissions to make a request ? – Jakub Pluta Apr 16 '21 at 16:33
  • It could be a security related issue with your setup. Have a read through here: https://airflow.apache.org/docs/apache-airflow/stable/security/index.html - I'd suggest creating a new question on stack overflow with more information about your issue, it will get more exposure and a better chance of an answer! If you do so, leave a link to the question here as I may be able to help with more info. – Josh Apr 18 '21 at 12:04
  • did you manage to address the issue? – Benni Oct 24 '22 at 16:40
  • Adding comment this is still true as of Airflow v2.4.1 as well – Matthew Barlowe Jan 26 '23 at 15:05
2

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – Tyler2P Nov 06 '21 at 17:39
  • this answer solves the problem. if this doesnt work out of the box, you have to take a look at the config file and change basic_auth to password_auth like this (under api section) [api] auth_backend = airflow.api.auth.backend.password_auth – Ala Ham Apr 13 '22 at 14:58