package main import ( "encoding/json" "fmt" "net/http" "net/url" "os" ) // JSONPayload represents the structure of the nested JSON for the API request. type JSONPayload struct { Query string `json:"query"` Sort string `json:"sort"` Set string `json:"set"` Filters []interface{} `json:"filters"` Limit int `json:"limit"` VariantLimit bool `json:"variantLimit"` CollectionLimit bool `json:"collectionLimit"` Cursor int `json:"cursor"` Direction string `json:"direction"` } // Input represents the top-level structure of the input JSON for the API request. type Input struct { Part0 struct { JSON JSONPayload `json:"json"` } `json:"0"` } func main() { // Parameterize the Limit and Cursor values limit := 35 cursor := 35 set := "bet" // Define the input data structure input := Input{ Part0: struct { JSON JSONPayload `json:"json"` }{ JSON: JSONPayload{ Query: "", Sort: "relevance", Set: set, Filters: []interface{}{}, Limit: limit, VariantLimit: false, CollectionLimit: false, Cursor: cursor, Direction: "forward", }, }, } // Serialize the input data structure to JSON jsonData, err := json.Marshal(input) if err != nil { logErrorAndExit(fmt.Errorf("failed to marshal input data: %w", err)) } // URL encode the JSON data urlEncodedInput := url.QueryEscape(string(jsonData)) // Print the URL-encoded JSON input for debugging fmt.Fprintln(os.Stderr, "URL-encoded JSON input:", urlEncodedInput) // Construct the full URL apiURL := fmt.Sprintf("https://curiosa.io/api/trpc/card.search?batch=1&input=%s", urlEncodedInput) // Print the full API URL for debugging fmt.Fprintln(os.Stderr, "Full API URL:", apiURL) // Create a new HTTP request req, err := http.NewRequest("GET", apiURL, nil) if err != nil { logErrorAndExit(fmt.Errorf("failed to create request: %w", err)) } // Set essential headers setHeaders(req) // Perform the HTTP request client := &http.Client{} resp, err := client.Do(req) if err != nil { logErrorAndExit(fmt.Errorf("failed to perform request: %w", err)) } defer resp.Body.Close() // Check for non-200 status codes if resp.StatusCode != http.StatusOK { logErrorAndExit(fmt.Errorf("non-200 response: %s", resp.Status)) } // Read and pretty print the JSON response var prettyJSON json.RawMessage err = json.NewDecoder(resp.Body).Decode(&prettyJSON) if err != nil { logErrorAndExit(fmt.Errorf("failed to decode response body: %w", err)) } prettyData, err := json.MarshalIndent(prettyJSON, "", " ") if err != nil { logErrorAndExit(fmt.Errorf("failed to marshal pretty JSON: %w", err)) } // Output the pretty JSON to stdout fmt.Println(string(prettyData)) } // setHeaders sets the necessary headers for the HTTP request. func setHeaders(req *http.Request) { req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:126.0) Gecko/20100101 Firefox/126.0") req.Header.Set("Accept", "*/*") req.Header.Set("content-type", "application/json") } // logErrorAndExit logs the error to stderr and exits with a non-zero status code. func logErrorAndExit(err error) { fmt.Fprintln(os.Stderr, err) os.Exit(1) }