fix: proxy-call location and values

This commit is contained in:
2025-10-19 22:00:17 +02:00
parent 5e1c39b0bf
commit cdf99718ae
3 changed files with 20 additions and 48 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
spore-gateway

View File

@@ -597,12 +597,20 @@ func (hs *HTTPServer) proxyCall(w http.ResponseWriter, r *http.Request) {
params := make(map[string]interface{})
for _, param := range requestBody.Params {
if name, ok := param["name"].(string); ok {
// Create parameter object with type and location info
// Create parameter object preserving UI-provided metadata
paramObj := map[string]interface{}{
"location": "body",
"location": "body", // default location
"type": "string", // default type
}
// Preserve the UI's location and type information
if location, ok := param["location"].(string); ok && location != "" {
paramObj["location"] = location
}
if paramType, ok := param["type"].(string); ok && paramType != "" {
paramObj["type"] = paramType
}
// Extract the actual value from the parameter object
if value, ok := param["value"]; ok {
paramObj["value"] = value
@@ -610,22 +618,8 @@ func (hs *HTTPServer) proxyCall(w http.ResponseWriter, r *http.Request) {
paramObj["value"] = param
}
// Check if we have type information from the endpoint definition
// For now, we'll detect JSON by checking if the value is a JSON string
if value, ok := paramObj["value"].(string); ok {
// Special handling for labels parameter - it expects raw JSON string
if name == "labels" {
paramObj["type"] = "json"
// Keep the value as string for labels parameter
} else {
// Try to parse as JSON to detect if it's a JSON parameter
var jsonValue interface{}
if err := json.Unmarshal([]byte(value), &jsonValue); err == nil {
paramObj["type"] = "json"
paramObj["value"] = jsonValue
}
}
}
// Keep the value as-is, don't try to auto-detect JSON
// The UI will specify the correct type, and the client will handle it appropriately
params[name] = paramObj
}

View File

@@ -269,24 +269,18 @@ func (c *SporeClient) buildProxyRequest(method, targetURL string, params map[str
var contentType string
if method != "GET" && params != nil {
// Check if we have JSON parameters
hasJSONParams := false
jsonParams := make(map[string]interface{})
// Use form-encoded data for all body parameters (matching index-standalone.js behavior)
form := make(map[string][]string)
query := make(map[string][]string)
for name, value := range params {
location := "body"
paramType := "string"
// Check if value is a parameter object with location and type info
// Check if value is a parameter object with location info
if paramObj, ok := value.(map[string]interface{}); ok {
if loc, exists := paramObj["location"].(string); exists {
location = loc
}
if ptype, exists := paramObj["type"].(string); exists {
paramType = ptype
}
// Extract the actual value
if val, exists := paramObj["value"]; exists {
value = val
@@ -307,17 +301,9 @@ func (c *SporeClient) buildProxyRequest(method, targetURL string, params map[str
targetURL = strings.ReplaceAll(targetURL, placeholder, fmt.Sprintf("%v", value))
}
default:
// Special handling for certain parameters that expect form-encoded data
// even when marked as "json" type
if paramType == "json" && name == "labels" {
// The labels parameter expects form-encoded data, not JSON
form[name] = append(form[name], fmt.Sprintf("%v", value))
} else if paramType == "json" {
hasJSONParams = true
jsonParams[name] = value
} else {
form[name] = append(form[name], fmt.Sprintf("%v", value))
}
// Use form-encoded data for all body parameters (matching index-standalone.js behavior)
// This is simpler and more reliable than trying to detect JSON automatically
form[name] = append(form[name], fmt.Sprintf("%v", value))
}
}
@@ -338,17 +324,8 @@ func (c *SporeClient) buildProxyRequest(method, targetURL string, params map[str
targetURL = urlObj.String()
}
// Create request body
if hasJSONParams {
// Send JSON body
jsonData, err := json.Marshal(jsonParams)
if err != nil {
return nil, fmt.Errorf("failed to marshal JSON params: %w", err)
}
body = strings.NewReader(string(jsonData))
contentType = "application/json"
} else if len(form) > 0 {
// Send form-encoded body
// Create request body - always use form-encoded data for consistency
if len(form) > 0 {
data := url.Values{}
for key, values := range form {
for _, value := range values {