1.1.1 Performing the HTTP GET

The following code will get a web page from an HTTP server and the pass it as a string to the display procedure. display will write the string to the current output port.

;; Fetch a web page and display it.  Requires guile-curl with Guile 2.2
;; or 3.0.
(use-modules (curl))

(define handle (curl-easy-init))
(curl-easy-setopt handle 'url "http://www.gnu.org")
(display (curl-easy-perform handle))

The returned string is also read in as binary and put into a string of 8-bit, single byte characters. But, since Guile’s characters are Unicode codepoints, this, in effect, means that the response was interpreted in the ISO-8859-1 (or Latin-1) encoding.

You can also fetch the resource as a bytevector by adding the optional argument #t to the call to curl-easy-perform. Fetching a result as a bytevector is useful as it can then be re-encoded into UTF-8 by using the utf8->string procedure or to any character encoding by using the bytevector->string procedure.

The following code will fetch a web page from an HTTP server as a bytevector, convert the bytevector to a string interpreting its contents as UTF-8 encoded data, and then pass the string to the display procedure. display will write the string to the current output port.

;; Fetch a web page as UTF-8 text and display it.  Requires guile-curl
;; with Guile 2.2 or 3.0.
(use-modules (curl)
             (rnrs bytevectors))

(setlocale LC_ALL "")
(define handle (curl-easy-init))
(curl-easy-setopt handle 'url "http://www.gnu.org")
(display (utf8->string (curl-easy-perform handle #t)))

In summary

When a transfer fails

If the transfer fails, curl-easy-perform returns #f instead of the resource. curl-error-code and curl-error-string then say why:

(let ((page (curl-easy-perform handle)))
  (if page
      (display page)
      (format #t "Transfer failed (~a): ~a~%"
              (curl-error-code) (curl-error-string))))

A common case is an https URL that fails while the same URL with http works, with an error about the server’s certificate (for example, CURLE_PEER_FAILED_VERIFICATION). This usually means libcurl cannot find the certificate authority (CA) certificates it needs to check the server’s certificate. Tell it where your system keeps them, with the cainfo option for a single bundle file or the capath option for a directory:

(curl-easy-setopt handle 'cainfo "/etc/ssl/certs/ca-certificates.crt")

After a transfer, (curl-easy-getinfo handle 'cainfo) and (curl-easy-getinfo handle 'capath) report the locations libcurl used by default (with libcurl 7.84.0 or later).

Setting the ssl-verifypeer option to #f also makes such an error go away, but only because it turns off the check that the server is the one it claims to be, which leaves the connection open to interception. Use it only for testing.

That’s pretty much all there is to it for a simple fetch. Beyond that, there are dozens of options that can be set by curl-easy-setopt that change how the request is performed. Easy-peasy.

But things get much, much worse if you don’t know the encoding of your data before hand and have to learn it from the HTTP server. In that case, things get complicated.