【发布时间】:2020-03-04 06:16:04
【问题描述】:
我正在尝试通过关注 https://airflow.apache.org/docs/stable/api.html 这个站点来触发 Airflow Dags。他们提供了一个 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()
}
【问题讨论】:
-
curl 请求后得到的输出是什么?检查服务器日志?