mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-25 11:31:46 +00:00
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"mime/multipart"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
)
|
|
|
|
func createInsructions(marshal []byte) (*bytes.Buffer, string, error) {
|
|
// Create a new form data object
|
|
body := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(body)
|
|
|
|
// Add the "instructions" key with the value of marshal
|
|
part, err := writer.CreateFormField("instructions")
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
part.Write(marshal)
|
|
|
|
// Close the multipart writer
|
|
writer.Close()
|
|
|
|
return body, writer.FormDataContentType(), nil
|
|
}
|
|
|
|
func sendRequest(client *resty.Client, url string, ins []interface{}) error {
|
|
// Manually marshal instructions
|
|
marshal, err := json.Marshal(ins)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create instructions form data
|
|
req, content, err := createInsructions(marshal)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Build and send request
|
|
res, err := client.R().
|
|
SetHeader("Content-Type", content).
|
|
SetBody(req.Bytes()).
|
|
Post(fmt.Sprintf("%s/instructions", url))
|
|
|
|
// Error handling
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Unmarshal response
|
|
var data Response
|
|
err = json.Unmarshal(res.Body(), &data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Print response
|
|
if !data.Success {
|
|
return errors.New("Remote server responded unsuccessfully!")
|
|
}
|
|
|
|
return nil
|
|
}
|