1

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 Answer 1

1

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\"}"}'
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! When I was composing the script, I made a mistake with escaping quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.