mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-26 03:51:45 +00:00
write marshaled string into form data
This commit is contained in:
parent
ed722f82e4
commit
85b96a5ea3
37
main.go
37
main.go
@ -1,10 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"mime/multipart"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@ -116,6 +118,25 @@ func LoadMatrixData() (string, int, int) {
|
|||||||
return url, width, height
|
return url, width, height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateInsructions(marshal []byte) (*bytes.Buffer, string) {
|
||||||
|
// 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 {
|
||||||
|
log.Fatal(err)
|
||||||
|
return nil, ""
|
||||||
|
}
|
||||||
|
part.Write(marshal)
|
||||||
|
|
||||||
|
// Close the multipart writer
|
||||||
|
writer.Close()
|
||||||
|
|
||||||
|
return body, writer.FormDataContentType()
|
||||||
|
}
|
||||||
|
|
||||||
func SendRequest(client *resty.Client, url string, ins []interface{}) {
|
func SendRequest(client *resty.Client, url string, ins []interface{}) {
|
||||||
// Manually marshal instructions
|
// Manually marshal instructions
|
||||||
marshal, err := json.Marshal(ins)
|
marshal, err := json.Marshal(ins)
|
||||||
@ -127,10 +148,16 @@ func SendRequest(client *resty.Client, url string, ins []interface{}) {
|
|||||||
// Debug log json data
|
// Debug log json data
|
||||||
log.Printf("Request: %s\n", string(marshal))
|
log.Printf("Request: %s\n", string(marshal))
|
||||||
|
|
||||||
|
// Create instructions form data
|
||||||
|
req, content := CreateInsructions(marshal)
|
||||||
|
if req == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Build and send request
|
// Build and send request
|
||||||
resp, err := client.R().
|
res, err := client.R().
|
||||||
SetHeader("Content-Type", "application/json").
|
SetHeader("Content-Type", content).
|
||||||
SetBody(ins).
|
SetBody(req.Bytes()).
|
||||||
Post(fmt.Sprintf("%s/instructions", url))
|
Post(fmt.Sprintf("%s/instructions", url))
|
||||||
|
|
||||||
// Error handling
|
// Error handling
|
||||||
@ -140,11 +167,11 @@ func SendRequest(client *resty.Client, url string, ins []interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Print response status
|
// Print response status
|
||||||
log.Printf("Response Status: %s\n", resp.Status())
|
log.Printf("Response Status: %s\n", res.Status())
|
||||||
|
|
||||||
// Unmarshal response
|
// Unmarshal response
|
||||||
var data Response
|
var data Response
|
||||||
err = json.Unmarshal(resp.Body(), &data)
|
err = json.Unmarshal(res.Body(), &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user