Skip to content
Oliver Eilhard edited this page Mar 2, 2015 · 12 revisions

For advanced scenarios, you can provide your own http.Client / http.Transport. You need to create your own http.Client, set its Transport field, then configure the new client with elastic.SetHttpClient(...). Here is an example that changes all GET requests to POST.

Warning This changes ALL GET requests to POST requests, which is probably not what you want with Elasticsearch. It's here to illustrate an example!

// GetToPostTransport will change all GET requests to POST.
type GetToPostTransport struct {
	next http.RoundTripper  // next round-tripper (use http.DefaultTransport if nil)
}

// RoundTrip implements a transport that will change all GET requests to POST.
func (tr *GetToPostTransport) RoundTrip(r *http.Request) (*http.Response, error) {
        if r.Method == "GET" {
                r.Method = "POST"
        }
	if tr.next != nil {
		return tr.next.RoundTrip(r)
	}
	return http.DefaultTransport.RoundTrip(r)
}

...
myHttpClient := &http.Client{Transport: &GetToPostTransport{}}
client, err := elastic.NewClient(elastic.SetHttpClient(myHttpClient))
Clone this wiki locally