Next: , Previous: , Up: Fetching from an HTTP server   [Contents][Index]


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.

;; This code requires Guile-1.8 or greater
(use-modules (curl))

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

In Guile-1.8, the returned string will be ‘8-bit clean’. This means that the request is read in binary and put into a string of 8-bit, single-byte characters.

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

In Guile-2.0, 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.

;; This code requires Guile-2.0+
(use-modules (curl))

(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

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.


Next: , Previous: , Up: Fetching from an HTTP server   [Contents][Index]