英文:
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}%
为什么会出现这种情况?
英文:
Here is my code, through which I want to check the cluster's health
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)
}
The error:
2021/08/11 12:33:47 Get "https://username:***@my.elasticsearch.com/_cluster/health": invalid request :path "_cluster/health"
exit status 1
When using 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}%
Why is that?
答案1
得分: 1
移除主机地址末尾的斜杠,并将其放在路径中。
英文:
Remove host trailing slash and put it in the path.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论