Plugins: Support set body content in plugin routes (#32551)

Adds support for overriding the body and length in plugin routes.
This commit is contained in:
Marcus Efraimsson
2021-03-31 16:38:35 +02:00
committed by GitHub
parent 027e886997
commit aad43869c3
9 changed files with 105 additions and 0 deletions
+16
View File
@@ -3,7 +3,9 @@ package pluginproxy
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"text/template"
"github.com/grafana/grafana/pkg/models"
@@ -60,6 +62,20 @@ func addQueryString(req *http.Request, route *plugins.AppPluginRoute, data templ
return nil
}
func setBodyContent(req *http.Request, route *plugins.AppPluginRoute, data templateData) error {
if route.Body != nil {
interpolatedBody, err := interpolateString(string(route.Body), data)
if err != nil {
return err
}
req.Body = ioutil.NopCloser(strings.NewReader(interpolatedBody))
req.ContentLength = int64(len(interpolatedBody))
}
return nil
}
// Set the X-Grafana-User header if needed (and remove if not)
func applyUserHeader(sendUserHeader bool, req *http.Request, user *models.SignedInUser) {
req.Header.Del("X-Grafana-User")