on 4fdc19d help sign in
87 lines 2.9 kB history raw
1+++
2title = "Example"
3weight = 4
4template = "doc.html"
5+++
7Here we'll look at a simple example of fetching a remote HTTP resource with Iris. We'll use the following thread, which you can save in the `/ted` directory of your `%base` desk:
9`iris-thread.hoon`
11```hoon
12/- spider
13/+ strandio
14=, strand=strand:spider
15^- thread:spider
16|= arg=vase
17=/ m (strand ,vase)
18^- form:m
19=/ url=@t (need !<((unit @t) arg))
20=/ =request:http [%'GET' url ~ ~]
21=/ =task:iris [%request request *outbound-config:iris]
22=/ =card:agent:gall [%pass /http-req %arvo %i task]
23;< ~ bind:m (send-raw-card:strandio card)
24;< res=(pair wire sign-arvo) bind:m take-sign-arvo:strandio
25?. ?=([%iris %http-response %finished *] q.res)
26 (strand-fail:strand %bad-sign ~)
27~& +.q.res
28?~ full-file.client-response.q.res
29 (strand-fail:strand %no-body ~)
30(pure:m !>(`@t`q.data.u.full-file.client-response.q.res))
31```
33This thread takes a fully qualified URL in a `@t` as an argument. It will ask Iris to fetch the HTTP resource at the given URL by passing it a [%request task](/docs/arvo/iris/tasks#request) containing an HTTP GET [$request:http](/docs/arvo/eyre/data-types#requesthttp):
35```hoon
36=/ url=@t (need !<((unit @t) arg))
37=/ =request:http [%'GET' url ~ ~]
38=/ =task:iris [%request request *outbound-config:iris]
39=/ =card:agent:gall [%pass /http-req %arvo %i task]
40```
42In this example, our `request:http` specifies no additional headers and has no body so it has a `~` for each of those fields. Of course in practice if you have headers or data you want to send you would include those.
44Our thread will take the `%http-response` `gift` that comes back from Iris and debug print it to the terminal so you can have a look at the structure, and then it will cast the body of the HTTP message to a `@t` and print it.
46Let's try it out:
48```
49> -iris-thread 'http://example.com'
50[ %http-response
51 client-response
52 [ %finished
53 response-header
54 [ status-code=200
55 headers
56 ~[
57 [key='age' value='212909']
58 [key='cache-control' value='max-age=604800']
59 [key='content-type' value='text/html; charset=UTF-8']
60 [key='date' value='Thu, 24 Jun 2021 04:12:13 GMT']
61 [key='expires' value='Thu, 01 Jul 2021 04:12:13 GMT']
62 [key='last-modified' value='Thu, 17 Oct 2019 07:18:26 GMT']
63 [key='server' value='ECS (oxr/8328)']
64 [key='vary' value='Accept-Encoding']
65 [key='x-cache' value='HIT']
66 [key='content-length' value='1256']
67 ]
68 ]
69 full-file
70 [ ~
71 [ type='text/html; charset=UTF-8'
72 data
73 [ p=1.256
74 q
75 224.708.415.080.409.844.273.808.970.700.472.455.882.111.359.8(...truncated for brevity)
76 ]
77 ]
78 ]
79 ]
81```
83...and here's the data from the HTTP response cast to a `@t`:
85```
86'<!doctype html>\0a<html>\0a<head>\0a <title>Example Domain</title>\0a\0a (...truncated for brevity)'
87```