Search code examples
httpgoelasticsearchhttprequest

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


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?


Solution

  • Remove host trailing slash and put it in the path.