我正在尝试通过https://airflow.apache.org/docs/stable/api.html这个站点来触发气流障碍。他们提供了curl命令curl -X POST \
http://localhost:8080/api/experimental/dags/<DAG_ID>/dag_runs \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{"conf":"{\"key\":\"value\"}"}'
来触发Dag。当我从Shell执行此命令时,它正在工作。
我尝试在Go POST /api/experimental/dags/<DAG_ID>/dag_runs
中调用此终结点,但得到了400 Bad Request
如何在Go中将POST /api/experimental/dags/<DAG_ID>/dag_runs
用作http.POST()
或http.NewRequest()
?
我尝试了这个:
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
func main(){
body := strings.NewReader(`{"conf":"{\"key\":\"value\"}"}`)
req, err := http.NewRequest("POST", "http://localhost:8080/api/experimental/dags/airflow_sample/dag_runs", body)
if err != nil {
fmt.Println(err)
}
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp)
defer resp.Body.Close()
}
最佳答案
我解决了
在主要功能中-
我更改了body := strings.NewReader(`{"conf":"{\"key\":\"value\"}"}`)
到这个body := strings.NewReader(`{}`)
它像魅力一样运作。
关于http - 如何使用REST API触发Airflow Dag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60520083/