Fetch & Curl

FETCH makes HTTP requests from within OcaltQL. It operates in two modes - synchronous by default, or asynchronous with SET PROMISE. All modifiers are optional and can appear in any order. CURL is a direct alias for FETCH.

Sync vs Async

This is the most important distinction in FETCH.

Synchronous - AFTER blocks until the fetch completes. The script cannot proceed until the response arrives. Simple and sequential, but the script pays the full wait cost at that line.

Asynchronous - SET PROMISE starts the fetch in the background immediately. The script continues running. The variable is WAITING until the response arrives. You only block when you explicitly call WAIT FOR - and only if the fetch has not already resolved by then.

Synchronous - Script Blocks Here
FETCH "https://example.ocalt.com/api/echo.php" SET ?data
AFTER EMIT ?data
(* AFTER cannot run until FETCH completes *)
Asynchronous - Script Continues Immediately
FETCH "https://example.ocalt.com/api/echo.php" SET PROMISE ?data
AFTER EMIT "I ran immediately while fetch is still in progress"
AFTER WAIT FOR ?data SET ?result
AFTER EMIT ?result
(* Script runs freely until WAIT FOR - only blocks there *)
Parallel Async Fetches - Maximum Efficiency
FETCH "https://api1.com/data" SET PROMISE ?a
AND FETCH "https://api2.com/data" SET PROMISE ?b
AND FETCH "https://api3.com/data" SET PROMISE ?c
(* All three start simultaneously in the background *)

AFTER EMIT "Doing other work while fetches run"
AFTER CALCULATE 1 + 1 SET ?result

(* Now collect - if already resolved, no wait at all *)
AFTER WAIT FOR ?a SET ?a
AFTER WAIT FOR ?b SET ?b
AFTER WAIT FOR ?c SET ?c
AFTER EMIT ?a & ?b & ?c
Use sync when the next statement depends directly on the fetch result and there is nothing else to do in between. Use async with PROMISE when you have other work to do, or when you are firing multiple fetches and want them to run in parallel.

Basic FETCH

Without modifiers, FETCH performs a GET request and captures the response body.

GET Request
STRING "https://example.ocalt.com/api/echo.php" SET ?url
AFTER FETCH ?url SET ?data
AFTER EMIT ?data

Modifiers

All modifiers are optional and can appear in any order after the URL.

METHOD
FETCH ?url METHOD "post" SET ?posted
IP - Route Through Specific IP
FETCH ?url METHOD "post" IP "192.168.1.1" SET ?proxied
USER AGENT
FETCH ?url METHOD "post" USER AGENT "Mozilla/5.0" SET ?spoofed
TIMEOUT
FETCH ?url USER AGENT "CustomBot/1.0" TIMEOUT 10 SECONDS SET ?slow
HEADER - Multiple Headers
FETCH ?url METHOD "get" HEADER "Authorization: Bearer token" HEADER "X-Custom: value" SET ?headers

Request Body

The BODY modifier passes a request body. Set the appropriate HEADER alongside it for the content type. A string is sent as its UTF-8 text. A bytes value, such as a binary file read with FILE READ, is sent as the exact bytes it holds, which is what an upload to object storage or a WebDAV PUT requires.

Form Body
FETCH ?url METHOD "post" BODY "name=Alice&age=30" SET ?form
JSON Body
FETCH ?url METHOD "post" BODY '{"name":"Alice"}' HEADER "Content-Type: application/json" SET ?json
Binary Body
FILE READ "/root/photo.jpg" SET ?photo
AFTER FETCH ?uploadUrl METHOD "put" BODY ?photo HEADER "Content-Type: image/jpeg" SET ?code WITH STATUS
AFTER EMIT ?code
(* The server receives the JPEG itself, byte for byte *)

Download to File

INTO writes the response body directly to a file path. Side-effect only - no SET. The file receives the response exactly as the server sent it, byte for byte, so an image, an archive or a PDF arrives intact.

A response captured with SET or WITH DATA follows the rule FILE READ follows: it is a string when the body is valid UTF-8 text, and a bytes value when it is not. A bytes response can be written with FILE WRITE, hashed with GENERATE HASH, or turned into base64 with ENCODE ... AS BASE64.

Fetch to File
FETCH ?url INTO "/root/downloaded.html"

Parsing the Response

PARSE JSON converts a raw JSON response string into an OcaltQL object.

FETCH and Parse JSON
FETCH ?url SET ?raw
AFTER PARSE JSON ?raw SET ?parsed
AFTER EMIT ?parsed("status")
AFTER EMIT ?parsed("data")

Split Status and Data Capture

To capture the HTTP status code and response body separately in one request, use WITH STATUS and WITH DATA.

Status and Body Separately
FETCH "https://example.ocalt.com/api/echo.php" SET ?responsecode WITH STATUS AND SET ?responsedata WITH DATA
AFTER EMIT ?responsecode
AFTER EMIT ?responsedata

CURL

CURL is a direct alias for FETCH. Every modifier and pattern available on FETCH works identically on CURL.

CURL - Basic
CURL ?url SET ?response
CURL - With Modifier
CURL ?url METHOD "get" SET ?simple
Modifier order does not matter. METHOD, HEADER, BODY, USER AGENT, IP, and TIMEOUT can appear in any combination and in any sequence after the URL.

CRAWL - the Link Tree of a Website

CRAWL starts at one page and follows its links, as deep as you say, and gives back every link it found: which page linked to which. It does not keep the pages themselves, only the map of links. Use it to see the shape of a site, check for broken links, or find the pages you want to FETCH next.

A site map, two levels deep
CRAWL "https://example.ocalt.com" DEPTH 2 SET ?tree
AFTER CAST ?tree AS JSON SET ?j
AFTER EMIT ?j

The answer is a tree. Each page has its address, the HTTP status it answered with, how deep it is, and the links found on it:

{"url": "https://example.ocalt.com/", "status": 200, "depth": 0, "links": [
  {"url": "https://example.ocalt.com/about", "status": 200, "depth": 1, "links": [ ... ]},
  {"url": "https://example.ocalt.com/brochure.pdf", "status": null, "depth": 1, "kind": "file", "links": []}
]}

Modifiers

ModifierWhat it does
DEPTH nHow many clicks away from the first page to go. 0 is the first page only. The default is 1; the most is 5.
LIMIT nThe most links to collect. The default is 500; the most is 5,000. CRAWL stops when it reaches it.
WITHIN DOMAINList only links on the same site. Without it, links to other sites are listed but never followed.
ANYWHEREFollow links to other sites too. Use it with a small DEPTH.
MATCH "pattern"Keep and follow only links whose address matches the pattern (a regular expression). MATCH "/docs/" keeps the documentation pages.
EXCLUDE "pattern"Drop links whose address matches the pattern. EXCLUDE "\\.(jpg|png|zip)$" leaves out pictures and archives.
FLATGive a plain list instead of a tree: one entry per link, with the page it was found on in "from".
TIMEOUT n SECONDSHow long to wait for each page. The default is 10 seconds. The whole crawl stops after 120 seconds and returns what it has.
Only the documentation, as a flat list
CRAWL "https://example.ocalt.com" DEPTH 3 WITHIN DOMAIN MATCH "/docs/" FLAT SET ?pages
AFTER COUNT ?pages SET ?n
AFTER EMIT ?n & " documentation pages"
Find broken links
CRAWL "https://example.ocalt.com" DEPTH 2 WITHIN DOMAIN FLAT SET ?links
AFTER FOREACH ?links SET ?l
OPEN
  IF ?l("status") IS GREATER THAN OR EQUAL TO 400
  OPEN
    EMIT ?l("status") & "  " & ?l("url") & "  (linked from " & ?l("from") & ")\n"
  CLOSE
CLOSE
How CRAWL behaves. It reads each page once, even when many pages link to it. It opens only web pages; files such as PDFs and pictures are listed with "kind": "file" and a status of null, without being downloaded. It follows a site's robots.txt. It never connects to private or local addresses. The whole crawl is one statement, so it counts as one query.