HTTP
Five members on the http namespace for outbound requests: http.get, http.post, http.put, http.delete, and http.request for arbitrary methods. Each returns a uniform response object with ok, status, headers, body, and error — branch on ok, read status and body when you need them. The implementation shares one connection pool across all calls, and arrays / objects passed as the body are auto-JSON-encoded with a Content-Type: application/json header.
http.get — fetch a URL
Server-side only; pure data shape
http.get(url, opts?) issues a GET request and returns the response object. opts is optional and accepts timeout (ms), headers (object), follow_redirects (bool), and body (rare for GET). Default timeout is 30 seconds.
> server
resp: http.get('https://api.github.com/repos/anthropics/anthropic-sdk-python')
ok: resp.ok
status: resp.status
> page
<p> [status] / ok? [ok] Response shape
Uniform regardless of method or status
Every response object has exactly five fields. ok is true when status is between 200 and 399. error is non-empty only when the transport itself failed (DNS, timeout, broken connection) — for those failures status is 0. The body is always a string; call json.parse(resp.body) when you expect JSON.
# resp shape:
# { <doc_example heading['http.post — JSON body'] caption['Object or array body → automatic JSON encoding']> <p> Passing an object or array as the body is the common case — Rat marshals it with <code>encoding/json</code> and sets <code>Content-Type: application/json</code> for you (unless you override the header). Pass a string body verbatim when you need form-encoded or pre-built payloads. <code_block> <rat_code> > server resp: http.post('https://httpbin.org/post', {hello: 'rat'}) > page <p> echo'd back: [resp.body] <doc_example heading['Custom headers and timeout'] caption['Third arg = opts object']> <p> The opts object on POST / PUT goes in the third slot (after URL and body); on GET / DELETE / request it's the second slot. <code>headers</code> merges with the defaults; explicit <code>Content-Type</code> wins over the auto-set one. <code_block> <rat_code> > server resp: http.post( 'https://api.example.com/items', {name: 'rat'}, { timeout: 5000, headers: {Authorization: 'Bearer ' + api_token}, }, ) <doc_example heading['http.request — arbitrary method'] caption['For PATCH, HEAD, OPTIONS, etc.']> <p> <code>http.request(method, url, opts?)</code> takes the method as a string. The body goes inside <code>opts.body</code> rather than a positional slot, so the call shape stays uniform for verbs that don't conventionally carry a body. <code_block> <rat_code> > server resp: http.request('PATCH', 'https://api.example.com/items/1', { body: {name: 'updated'}, headers: {Authorization: 'Bearer ' + api_token}, }) <doc_example heading['Disabling redirect-following'] caption['follow_redirects: false to stop at 3xx']> <p> By default <code>http</code> follows redirects up to the standard <code>net/http</code> limit. Setting <code>follow_redirects: false</code> returns the 3xx response untouched — useful when you want to inspect the <code>Location</code> header without chasing it. <code_block> <rat_code> > server resp: http.get('https://example.com/old-path', {follow_redirects: false}) next: resp.headers['location'] <doc_example heading['Composing with json'] caption['JSON in, JSON out, idiomatic two-liner']> <p> The standard pattern: pass an object body (auto-encoded), parse the response body. The connection pool is reused for repeat calls to the same host, so successive requests are cheap. <code_block> <rat_code> > server resp: http.post('https://api.example.com/echo', {hello: 'rat'}) data: json.parse(resp.body) > page <p> server saw: [data.hello] <p> That closes the builtin tour. Next up: the <a href['/documentation/namespaces/json']> namespaces — <code>json</code>, <code>date</code>, <code>regex</code> — which use the same dotted dispatch you just saw on <code>http</code>.