使用客户端和 url 构造执行 go http get 请求时请求路径无效
Invalid request path when performing a go http get request using client and url constructs
这是我的代码,我想通过它检查集群的健康状况
client := http.Client{
Timeout: 5 * time.Second,
}
u := &url.URL{
Scheme: "https",
User: url.UserPassword("username", "pass"),
Host: "my.elasticsearch.com/",
Path: "_cluster/health",
}
req := http.Request{
URL: u,
}
resp, err := client.Do(&req)
if err != nil {
log.Fatal(err)
}
错误:
2021/08/11 12:33:47 Get "https://username:***@my.elasticsearch.com/_cluster/health": invalid request :path "_cluster/health"
exit status 1
使用 curl 时:
▶ curl -s -XGET https://my.elasticsearch.com/_cluster/health -u username:pass
{"cluster_name":"my-cluster","status":"green","timed_out":false,"number_of_nodes":4,"number_of_data_nodes":4,"active_primary_shards":9,"active_shards":18,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":100.0}%
这是为什么?
删除主机结尾的斜杠并将其放入路径中。
这是我的代码,我想通过它检查集群的健康状况
client := http.Client{
Timeout: 5 * time.Second,
}
u := &url.URL{
Scheme: "https",
User: url.UserPassword("username", "pass"),
Host: "my.elasticsearch.com/",
Path: "_cluster/health",
}
req := http.Request{
URL: u,
}
resp, err := client.Do(&req)
if err != nil {
log.Fatal(err)
}
错误:
2021/08/11 12:33:47 Get "https://username:***@my.elasticsearch.com/_cluster/health": invalid request :path "_cluster/health"
exit status 1
使用 curl 时:
▶ curl -s -XGET https://my.elasticsearch.com/_cluster/health -u username:pass
{"cluster_name":"my-cluster","status":"green","timed_out":false,"number_of_nodes":4,"number_of_data_nodes":4,"active_primary_shards":9,"active_shards":18,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":100.0}%
这是为什么?
删除主机结尾的斜杠并将其放入路径中。