Forum Discussion

chelusco's avatar
chelusco
Icon for Altostratus rankAltostratus
Dec 13, 2024

Cannot Figure out GO payload for XC Volterra API

I have been trying to send a body I am creating from a jinja template for creating an Origin Pool. I am using Go so I use gonja but either way, the template loads correctly.

However, I cannot seem to correctly send it via a payload and continuously get the error: json: cannot unmarshal string into Go value of type map[string]json.RawMessage.

This is the code snippet:

   payload := bytes.NewBuffer([]byte(jsonString))

   fmt.Println("Type of jsonObject:", reflect.TypeOf(payload))

   req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, payload)

The type returned is: Type of jsonObject: *bytes.Buffer so i do not know what else I would send?

  • I don’t know Go well, so I can’t tell where the error is happening. I’m not sure if the return type is from the Go program or from the XC API. By my likely very wrong assumptions and converting to python to understand and then reconverted back to go, perhaps some more robust json handling before the request would help? Here's what I got from chatGPT:

    package main
    
    import (
    	"bytes"
    	"encoding/json"
    	"fmt"
    	"net/http"
    	"reflect"
    )
    
    func main() {
    	// Example JSON input (replace this with your actual JSON string)
    	jsonString := `{"key1": "value1", "key2": {"nestedKey": "nestedValue"}}`
    
    	// Debug: Print the JSON structure
    	fmt.Println("JSON String:", jsonString)
    
    	// Create a bytes buffer for the payload
    	payload := bytes.NewBuffer([]byte(jsonString))
    
    	// Print the type of payload
    	fmt.Println("Type of payload:", reflect.TypeOf(payload))
    
    	// Try to decode the JSON into a map
    	var data map[string]json.RawMessage
    	err := json.Unmarshal([]byte(jsonString), &data)
    	if err != nil {
    		fmt.Println("Error unmarshaling JSON:", err)
    		// Handle other JSON types if needed
    		// Example: if the JSON is a string
    		var simpleString string
    		err = json.Unmarshal([]byte(jsonString), &simpleString)
    		if err == nil {
    			fmt.Println("JSON is a simple string:", simpleString)
    		} else {
    			// Handle additional cases as needed
    			fmt.Println("Unhandled JSON structure:", err)
    		}
    		return
    	}
    
    	// Successfully unmarshaled into a map
    	fmt.Println("Parsed JSON data:", data)
    
    	// Create an HTTP request
    	apiURL := "https://example.com/api" // Replace with your actual API URL
    	req, err := http.NewRequest(http.MethodPost, apiURL, payload)
    	if err != nil {
    		fmt.Println("Error creating request:", err)
    		return
    	}
    
    	// Debug: Print the request
    	fmt.Println("HTTP Request created:", req)
    }
    

     

    • JRahm's avatar
      JRahm
      Icon for Admin rankAdmin

      also in XC, if you go to the JSON tab in any workload or object you've created, you should have a good example to work from