http.getAsStream()
http.getAsStream()
Description
Parameters
| Parameter | Type | Explanation |
|---|---|---|
| url | String | The url to put data to |
| body | String | The data to put |
Example
url = "http://localhost:3000/d"
try {
http = option.rest
res = http.getAsStream(url)
// Stream the HTTP response to avoid loading the full payload into memory
// Optional: provide a root node name to start streaming from (e.g. "results")
stream = parser.asStream(res.getContent())
// Prime the iterator before entering the loop
resultMap = stream.next()
i = 0
// Stop when stream is exhausted or optional maxItems limit is reached
while (resultMap && (!maxItems || i < maxItems)) {
i++
// Periodic progress logging for long-running imports
if (i % 500 == 0) log.debug("${i} messages processed")
try {
// Process single record (business logic)
} finally {
// Ensure per-record cleanup even if processing fails
}
// Fetch next record only after current one has been fully processed
resultMap = stream.next()
}
// No data returned by endpoint
if (i == 0) {
log.debug("No records found in response from ${url}")
}
} catch (Exception e) {
log.error("Exception processing records from ${url}: ${e.message}")
} finally {
// Ensure HTTP stream is closed even on error
try { res.close() } catch (Exception e) {}
}