2
0
mirror of https://github.com/xcat2/confluent.git synced 2025-09-24 19:18:11 +00:00

Add ability to post bodys to HTTP requests

This commit is contained in:
Jarrod Johnson
2025-03-11 15:35:35 -04:00
parent 9cecaab055
commit 9123d2f2e0
2 changed files with 22 additions and 9 deletions

View File

@@ -72,22 +72,23 @@ func NewApiClient(cafile string, keyfile string, nodename string, server string)
func (apiclient *ApiClient) RegisterKey(crypted string, hmac string) (error) {
cryptbytes := []byte(crypted)
_, err := apiclient.request("/confluent-api/self/registerapikey", "", &cryptbytes, "", hmac)
cryptbuffer := bytes.NewBuffer(cryptbytes)
_, err := apiclient.request("/confluent-api/self/registerapikey", "", cryptbuffer, "", hmac)
return err
}
func (apiclient *ApiClient) Fetch(url string, outputfile string, mime string) (error) {
func (apiclient *ApiClient) Fetch(url string, outputfile string, mime string, body io.Reader) (error) {
outp, err := os.Create(outputfile)
if err != nil { return err }
defer outp.Close()
rsp, err := apiclient.request(url, mime, nil, "", "")
rsp, err := apiclient.request(url, mime, body, "", "")
if err != nil { return err }
_, err = io.Copy(outp, rsp)
return err
}
func (apiclient *ApiClient) GrabText(url string, mime string) (string, error){
rsp, err := apiclient.request(url, mime, nil, "", "")
func (apiclient *ApiClient) GrabText(url string, mime string, body io.Reader) (string, error){
rsp, err := apiclient.request(url, mime, body, "", "")
if err != nil { return "", err }
rspdata, err := io.ReadAll(rsp)
if err != nil { return "", err }
@@ -95,7 +96,7 @@ func (apiclient *ApiClient) GrabText(url string, mime string) (string, error){
return rsptxt, nil
}
func (apiclient *ApiClient) request(url string, mime string, body *[]byte, method string, hmac string) (io.ReadCloser, error) {
func (apiclient *ApiClient) request(url string, mime string, body io.Reader, method string, hmac string) (io.ReadCloser, error) {
if ! strings.Contains(url, "https://") {
url = fmt.Sprintf("https://%s%s", apiclient.urlserver, url)
}
@@ -111,7 +112,7 @@ func (apiclient *ApiClient) request(url string, mime string, body *[]byte, metho
if body == nil {
rq, err = http.NewRequest(method, url, nil)
} else {
rq, err = http.NewRequest(method, url, bytes.NewBuffer(*body))
rq, err = http.NewRequest(method, url, body)
}
if err != nil { return nil, err }
if (mime != "") { rq.Header.Set("Accept", mime) }

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"flag"
"os"
"io"
"fmt"
)
@@ -48,6 +49,8 @@ func main() {
invokeapi.BoolVar(&usejson, "j", false, "Request JSON formatted reply")
outputfile := invokeapi.String("o", "", "Filename to store download to")
invokeapi.StringVar(&confluentsrv, "s", "", "Confluent server to request from")
invokedata := invokeapi.String("d", "", "Data to submit")
invokedatafile := invokeapi.String("i", "", "File containing data to submit")
@@ -74,7 +77,16 @@ func main() {
outp.Write([]byte(password))
case "invoke":
var err error
var body io.Reader
body = nil
invokeapi.Parse(os.Args[2:])
if *invokedata != "" {
body = bytes.NewBuffer([]byte(*invokedata))
}
if *invokedatafile != "" {
body, err = os.Open(*invokedatafile)
if err != nil { panic(err) }
}
if confluentsrv == "" {
confluentsrv, err = get_confluent_server()
}
@@ -85,9 +97,9 @@ func main() {
mime = "application/json"
}
if *outputfile != "" {
apiclient.Fetch(invokeapi.Arg(0), *outputfile, mime)
apiclient.Fetch(invokeapi.Arg(0), *outputfile, mime, body)
}
rsp, err := apiclient.GrabText(invokeapi.Arg(0), mime)
rsp, err := apiclient.GrabText(invokeapi.Arg(0), mime, body)
if err != nil { panic(err) }
fmt.Println(rsp)
default: