I need help just getting a heartbeat with API v2 when attempting to fetch page content. Any example of working code that would be the equivalent for the following would be appreciated!
All previous efforts have resulted in pages with content being reported to have none.
The legacy code below works just fine.
Map<String, Object> performApiRequest(String endpoint, Map<String, String> queryParams) {
if (!endpoint || !(queryParams instanceof Map)) {
throw new IllegalArgumentException("Invalid endpoint or query parameters. Ensure both are provided correctly.")
}
println("INFO: Initiating API request to endpoint: ${endpoint}")
try {
// Perform the GET request
def response = get(endpoint)
.queryString(queryParams as Map)
.header("Content-Type", "application/json")
.asObject(String) // Response body is expected as a raw JSON string
// Check for a successful response status
if (response?.status == 200) {
println("INFO: API call to ${endpoint} succeeded with status: 200")
// Parse JSON response body to a Map
def body = response.body
if (body && body instanceof String) {
return new JsonSlurper().parseText(body) as Map<String, Object>
} else {
println("ERROR: Response body is missing or invalid.")
return null
}
} else {
println("ERROR: API call to ${endpoint} failed with status: ${response?.status ?: 'unknown'} - ${response?.statusText ?: 'unknown'}")
}
} catch (Exception e) {
println("ERROR: Exception during API call to ${endpoint}: ${e.message}")
}
return null // Return null if any part of the process fails
}
Thank you.