Lesson 13: HTTP Networking

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.

http-get

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"

http-post

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" -----------/

Working with APIs

A typical API workflow combines several nodes into a pipeline:

  1. Build the URL (concatenate base URL with path and query parameters).
  2. Call http-get or http-post.
  3. Parse the response with json-parse.
  4. Extract the fields you need with get.
  constant "https://api.weather.com/forecast?city=" --> concat <-- constant "London"
    --> http-get
    --> json-parse
    --> get "temperature"
    --> log

Error handling with try

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.

What you learned