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{}) params := make(map[string]interface{})
for _, param := range requestBody.Params { for _, param := range requestBody.Params {
if name, ok := param["name"].(string); ok { 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{}{ paramObj := map[string]interface{}{
"location": "body", "location": "body", // default location
"type": "string", // default type "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 // Extract the actual value from the parameter object
if value, ok := param["value"]; ok { if value, ok := param["value"]; ok {
paramObj["value"] = value paramObj["value"] = value
@@ -610,22 +618,8 @@ func (hs *HTTPServer) proxyCall(w http.ResponseWriter, r *http.Request) {
paramObj["value"] = param paramObj["value"] = param
} }
// Check if we have type information from the endpoint definition // Keep the value as-is, don't try to auto-detect JSON
// For now, we'll detect JSON by checking if the value is a JSON string // The UI will specify the correct type, and the client will handle it appropriately
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
}
}
}
params[name] = paramObj params[name] = paramObj
} }

View File

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