执行go http get请求时,使用客户端和URL构造时出现无效的请求路径。

huangapple go评论78阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2021年8月11日 17:48:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/68739689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定