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

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

Invalid request path when performing a go http get request using client and url constructs

问题

这是我的代码,我想通过它来检查集群的健康状态:

  1. client := http.Client{
  2. Timeout: 5 * time.Second,
  3. }
  4. u := &url.URL{
  5. Scheme: "https",
  6. User: url.UserPassword("username", "pass"),
  7. Host: "my.elasticsearch.com/",
  8. Path: "_cluster/health",
  9. }
  10. req := http.Request{
  11. URL: u,
  12. }
  13. resp, err := client.Do(&req)
  14. if err != nil {
  15. log.Fatal(err)
  16. }

错误信息:

  1. 2021/08/11 12:33:47 Get "https://username:***@my.elasticsearch.com/_cluster/health": invalid request :path "_cluster/health"
  2. exit status 1

当使用curl时:

  1. curl -s -XGET https://my.elasticsearch.com/_cluster/health -u username:pass
  2. {"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

  1. client := http.Client{
  2. Timeout: 5 * time.Second,
  3. }
  4. u := &url.URL{
  5. Scheme: "https",
  6. User: url.UserPassword("username", "pass"),
  7. Host: "my.elasticsearch.com/",
  8. Path: "_cluster/health",
  9. }
  10. req := http.Request{
  11. URL: u,
  12. }
  13. resp, err := client.Do(&req)
  14. if err != nil {
  15. log.Fatal(err)
  16. }

The error:

  1. 2021/08/11 12:33:47 Get "https://username:***@my.elasticsearch.com/_cluster/health": invalid request :path "_cluster/health"
  2. exit status 1

When using curl:

  1. curl -s -XGET https://my.elasticsearch.com/_cluster/health -u username:pass
  2. {"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:

确定