forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
force_http_get.go
30 lines (25 loc) · 1.03 KB
/
force_http_get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package middleware
import (
"net/http"
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/infra/log"
)
func ForceHttpGet(logger log.Logger) sdkhttpclient.Middleware {
return sdkhttpclient.NamedMiddlewareFunc("force-http-get", func(opts sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper {
// the prometheus library we use does not allow us to set the http method.
// it's behavior is to first try POST, and if it fails in certain ways
// (for example, by returning a method-not-allowed error), it will try GET.
// so here, we check if the http-method is POST, and if it is, we
// return an artificial method-not-allowed response.
// this will cause the prometheus library to retry with GET.
return sdkhttpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
if req.Method == http.MethodPost {
resp := &http.Response{
StatusCode: http.StatusMethodNotAllowed,
}
return resp, nil
}
return next.RoundTrip(req)
})
})
}