Advanced
Phograph can talk to web APIs using built-in HTTP nodes. Combined with JSON parsing (Lesson 10), you can fetch remote data, post updates, and integrate with any REST service.
The http-get node takes a URL string and returns the response body.
If the request succeeds (HTTP 2xx), the body is returned as a string. If it
fails, the node produces an error.
constant "https://api.example.com/users/1" --> http-get --> json-parse --> get "name"
outputs: "Alice"
The http-post node sends data to a URL. It takes two inputs:
the URL and the request body (typically a JSON string). The response body is
returned as a string.
dict ["name","Bob","role","admin"] --> json-encode --\
--> http-post --> log
constant "https://api.example.com/users" -----------/
A typical API workflow combines several nodes into a pipeline:
http-get or http-post.json-parse.get. constant "https://api.weather.com/forecast?city=" --> concat <-- constant "London"
--> http-get
--> json-parse
--> get "temperature"
--> log
Network requests can fail — the server might be down, the URL might be
wrong, or the response might not be valid JSON. The try annotation
wraps a node so that errors are caught instead of crashing the program.
constant "https://bad-url.example" --> [http-get (try)]
success --> json-parse --> log
failure --> log "Request failed"
When the try annotation is applied, the node produces two
outputs: a success path and a failure path. You wire downstream logic to
whichever path you need.
http-get fetches a URL and returns the response body as a string.http-post sends data to a URL and returns the response.json-parse to work with REST APIs.try annotation provides error handling for nodes that may fail.