Creating json variable with Airflow web API

131 views Asked by At

I'm trying to create variable with json value in Airflow using web api.

My script:

curl -X POST "${AIRFLOW_URL}/api/v1/variables" \
        -H "Content-Type: application/json" \
        --user "${AIRFLOW_USERNAME}:${AIRFLOW_PASSWORD}" \
        -d '{"key": "my_json_var", "value": {"key1":"val1","key2":"123"}}'

But it doesn't work. I get 400 status:

{
  "detail": "{'key1': 'val1', 'key2': '123'} is not of type 'string' - 'value'",
  "status": 400,
  "title": "Bad Request",
  "type": "https://airflow.apache.org/docs/apache-airflow/2.7.1/stable-rest-api-ref.html#section/Errors/BadRequest"
}

What am I doing wrong?

ps. I tried using python with requests and got the same result. I think the problem is in the airflow web api. Perhaps their methods don't support adding json variables

1

There are 1 answers

1
Pankaj Singh On BEST ANSWER

No problem with airflow API it accepts a variable value as a string as described in the documentation you can check https://airflow.apache.org/docs/apache-airflow/stable/stable-rest-api-ref.html#operation/post_variables

You should change the variable value to a string and it should work

curl -X POST ${AIRFLOW_URL}/api/v1/variables \
        -H "Content-Type: application/json" \
        --user "${AIRFLOW_USERNAME}:${AIRFLOW_PASSWORD}" \
        -d '{"key": "my_json_var", "value": "{\"key1\":\"val1\",\"key2\":\"123\"}"}'