@@ -5,18 +5,14 @@ import (
55 "encoding/json"
66 "fmt"
77 "io"
8- "log"
98 "net/http"
10- "net/url"
11- "strings"
129)
1310
1411// ProxmoxAPIRequest represents a request to the Proxmox API
1512type ProxmoxAPIRequest struct {
1613 Method string // GET, POST, PUT, DELETE
1714 Endpoint string // The API endpoint (e.g., "/nodes", "/nodes/node1/status")
1815 RequestBody any // Optional request body for POST/PUT requests
19- UseFormData bool // Whether to send as form data instead of JSON
2016}
2117
2218// ProxmoxAPIResponse represents the generic Proxmox API response structure
@@ -43,59 +39,21 @@ func NewProxmoxRequestHelper(baseURL, apiToken string, httpClient *http.Client)
4339// MakeRequest performs an HTTP request to the Proxmox API and returns the raw response data
4440func (prh * ProxmoxRequestHelper ) MakeRequest (req ProxmoxAPIRequest ) (json.RawMessage , error ) {
4541 var reqBody io.Reader
46- var contentType string
4742
4843 // Prepare request body for POST/PUT requests
4944 if req .Method == "POST" || req .Method == "PUT" {
5045 var bodyData any
5146 if req .RequestBody != nil {
52- log .Printf ("Request Body: %+v" , req .RequestBody )
5347 bodyData = req .RequestBody
5448 } else {
5549 bodyData = map [string ]any {}
5650 }
5751
58- if req .UseFormData {
59- // Convert to form data
60- formData := url.Values {}
61- if bodyMap , ok := bodyData .(map [string ]any ); ok {
62- for key , value := range bodyMap {
63- switch v := value .(type ) {
64- case string :
65- formData .Set (key , v )
66- case []string :
67- // For arrays, Proxmox expects multiple form fields with same name
68- for _ , item := range v {
69- formData .Add (key , item )
70- }
71- case []any :
72- // Handle generic interface slice (convert to strings)
73- for _ , item := range v {
74- if str , ok := item .(string ); ok {
75- formData .Add (key , str )
76- } else {
77- formData .Add (key , fmt .Sprintf ("%v" , item ))
78- }
79- }
80- default :
81- // Convert to JSON string for complex types
82- jsonBytes , _ := json .Marshal (v )
83- formData .Set (key , string (jsonBytes ))
84- }
85- }
86- }
87- reqBody = strings .NewReader (formData .Encode ())
88- contentType = "application/x-www-form-urlencoded"
89- log .Printf ("Form data: %s" , formData .Encode ())
90- } else {
91- // Send as JSON
92- jsonData , err := json .Marshal (bodyData )
93- if err != nil {
94- return nil , fmt .Errorf ("failed to marshal request body: %w" , err )
95- }
96- reqBody = bytes .NewBuffer (jsonData )
97- contentType = "application/json"
52+ jsonData , err := json .Marshal (bodyData )
53+ if err != nil {
54+ return nil , fmt .Errorf ("failed to marshal request body: %w" , err )
9855 }
56+ reqBody = bytes .NewBuffer (jsonData )
9957 }
10058
10159 // Create the full URL
@@ -109,9 +67,7 @@ func (prh *ProxmoxRequestHelper) MakeRequest(req ProxmoxAPIRequest) (json.RawMes
10967
11068 // Set headers
11169 httpReq .Header .Add ("Authorization" , "PVEAPIToken=" + prh .APIToken )
112- if req .Method == "POST" || req .Method == "PUT" {
113- httpReq .Header .Add ("Content-Type" , contentType )
114- }
70+ httpReq .Header .Add ("Content-Type" , "application/json" )
11571
11672 // Execute the request
11773 resp , err := prh .HTTPClient .Do (httpReq )
0 commit comments