2024-04-12 12:13:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"mime/multipart"
|
|
|
|
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
)
|
|
|
|
|
2024-04-15 15:35:02 +00:00
|
|
|
func createInsructions(marshal []byte) (*bytes.Buffer, string, error) {
|
2024-04-12 12:13:21 +00:00
|
|
|
// 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 {
|
2024-04-15 15:35:02 +00:00
|
|
|
return nil, "", err
|
2024-04-12 12:13:21 +00:00
|
|
|
}
|
|
|
|
part.Write(marshal)
|
|
|
|
|
|
|
|
// Close the multipart writer
|
|
|
|
writer.Close()
|
|
|
|
|
2024-04-15 15:35:02 +00:00
|
|
|
return body, writer.FormDataContentType(), nil
|
2024-04-12 12:13:21 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 15:35:02 +00:00
|
|
|
func sendRequest(client *resty.Client, url string, ins []interface{}) error {
|
2024-04-12 12:13:21 +00:00
|
|
|
// Manually marshal instructions
|
|
|
|
marshal, err := json.Marshal(ins)
|
|
|
|
if err != nil {
|
2024-04-15 15:35:02 +00:00
|
|
|
return err
|
2024-04-12 12:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create instructions form data
|
2024-04-15 15:35:02 +00:00
|
|
|
req, content, err := createInsructions(marshal)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-04-12 12:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2024-04-15 15:35:02 +00:00
|
|
|
return err
|
2024-04-12 12:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal response
|
|
|
|
var data Response
|
|
|
|
err = json.Unmarshal(res.Body(), &data)
|
|
|
|
if err != nil {
|
2024-04-15 15:35:02 +00:00
|
|
|
return err
|
2024-04-12 12:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print response
|
|
|
|
if !data.Success {
|
2024-04-15 15:35:02 +00:00
|
|
|
return errors.New("Remote server responded unsuccessfully!")
|
2024-04-12 12:13:21 +00:00
|
|
|
}
|
2024-04-15 15:35:02 +00:00
|
|
|
|
|
|
|
return nil
|
2024-04-12 12:13:21 +00:00
|
|
|
}
|