This package contains a HTTP client class, and related other classes.

The central class is http.HttpClient. Request objects are created (http.HttpRequest), passed to the client, and responses received (http.HttpResponse).

This is illustrated in the following simple program :-

Download http.icn

import net, http, io, lang

procedure main(a)
   local url, hreq, hc, hres, out

   a := a[1] | stop("URL expected")
   url := URL(a) | stop("Invalid url: " || a)
   url.scheme == ("http"|"https") | stop("Inappropriate url scheme: " || url.scheme)

   # A stream for the resulting data
   out := StringStream()

   # A request object
   hreq := HttpRequest().

   hc := HttpClient()

   # Get the response object
   hres := hc.retrieve(hreq) | stop(&why)

   write("OK ", *out.str(), " bytes\n", to_string(hres, 3))

This produces output similar to the following :-

$ ./http
OK 10611 bytes
object http.HttpResponse#2(
   url=object net.URL#3(
   status="HTTP/1.1 200 OK"
      object http.HttpResponse#1(
         url=object net.URL#1(
         status="HTTP/1.1 302 Found"
            object datastruct.SortTable#3(
               "Content-Type"->list#112["text/html; charset=UTF-8"]
               "Date"->list#115["Fri, 02 Sep 2016 12:33:16 GMT"]
      object datastruct.SortTable#4(
         "Cache-Control"->list#133["private, max-age=0"]
         "Content-Type"->list#134["text/html; charset=ISO-8859-1"]
         "Date"->list#131["Fri, 02 Sep 2016 12:33:16 GMT"]
         "P3P"->list#135["CP=\"This is not a P3P policy! See for more info.\""]
         "Set-Cookie"->list#141["NID=85=aerKk58-jVx9pLIo-Yq-W6pRf28JDj8mmP1OvSPg19CMvjDIqYtGIDqiG1f34v395bbyP2Cqy8mA9qS4sFBKqeexxx-V0yeaxhluXCJOxssVFRBwzyfoddmIZ3BY2YfH; expires=Sat, 04-Mar-2017 12:33:16 GMT; path=/;; HttpOnly"]
         "X-XSS-Protection"->list#139["1; mode=block"]

Example programs

The examples directory contains two programs which use HttpClient. The first is geturl, which is a command-line tool to retrieve URLs. The second is browser, which is a simple web browser. It makes use of ipl.browser.Browser, which uses multiple asynchronous HttpClients (each running in an io.Task) to download a particular web page and its images.