Help language development. Donate to The Perl Foundation

LibCurl cpan:CTILMES last updated on 2021-03-02
# Raku LibCurl

[![Build Status](](

[Simple Examples](#simple-examples)
[Header Options](#header-options)
[Special Options](#special-options)
[Received headers](#received-header-fields)

A [Raku]( interface to

    libcurl is a free and easy-to-use client-side URL transfer
    library, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS,
    SMTP, SMTPS, Telnet and TFTP. libcurl supports SSL certificates,
    HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload,
    proxies, cookies, user+password authentication (Basic, Digest,
    NTLM, Negotiate, Kerberos), file transfer resume, http proxy
    tunneling and more!

    libcurl is highly portable, it builds and works identically on
    numerous platforms, including Solaris, NetBSD, FreeBSD, OpenBSD,
    Darwin, HPUX, IRIX, AIX, Tru64, Linux, UnixWare, HURD, Windows,
    Amiga, OS/2, BeOs, Mac OS X, Ultrix, QNX, OpenVMS, RISC OS, Novell
    NetWare, DOS and more...

    libcurl is free, thread-safe, IPv6 compatible, feature rich, well
    supported, fast, thoroughly documented and is already used by many
    known, big and successful companies and numerous applications.

## Simple Examples

    use LibCurl::Easy;

    # GET
    print => '').perform.content;

    # GET (download a file) => '',
                      download => 'somefile').perform;

    # HEAD
    say, URL => '')

    # PUT (upload a file) => '',
                      upload => 'somefile').perform;

    # PUT (content from a string) => '',
                      send => 'My Content').perform;

     # DELETE => '',
                      customrequest => 'DELETE').perform;

    # POST => '',
                      postfields => 'name=foo&opt=value').perform;

## LibCurl::HTTP

If even those aren't easy enough, there is a tiny sub-class
**LibCurl::HTTP** that adds aliases for the common HTTP methods:

    use LibCurl::HTTP;

    my $http =;

    say $http.GET('').perform.content;

    say $http.GET('', 'myfile').perform.response-code;

    say $http.HEAD('').perform.response-code;


    $http.PUT('', 'myfile').perform;

    $http.POST('', 'name=foo&opt=value').perform;

LibCurl::HTTP methods also enable `failonerror` by default, so any
HTTP response >= 400 will throw an error.

You can even import very simple subroutines ala [WWW](

    use LibCurl::HTTP :subs;

    # Just GET content (will return Failure on failure):
    say get '';

    # GET and decode received data as JSON:
    say jget('')<args><foo>;

    # POST content (query args are OK; pass form as named args)
    say post '', :some<form>, :42args;

    # And if you need headers, pass them inside a positional Hash:
    say post '', %(:Some<Custom-Header>),
        :some<form>, :42args;

    # Same POST as above + decode response as JSON
    say jpost('', :some<form-arg>)<form><some>;

## Fancier Example

    my $curl =, :followlocation);
    $curl.setopt(URL => '', download => './myfile.html');
    say $curl.success;
    say $curl.Content-Type;
    say $curl.Content-Length;
    say $curl.Date;
    say $curl.response-code;
    say $curl.statusline;

Of course the full power of [libcurl]( is
available, so you aren't limited to HTTP URLs, you can use FTP, SMTP,
TFTP, SCP, SFTP, and many, many more.

## Options

Many of the libcurl
[options]( are
available, mostly just skip the ```CURLOPT_```, lowercase, and use '-'
instead of '_'.  For example, instead of
```CURLOPT_ACCEPT_ENCODING```, use ```accept-encoding```.  When the
options are really boolean (set to 1 to enable and 0 to disable), you
can treat them like option booleans if you want, ```:option``` to
enable, and ```:!option``` to disable.

Just like libcurl, the primary option is
[URL](, and can take
many forms depending on the desired protocol.

Options can be set on a handle either on initial creation with
```new()```, or later with ```.setopt()```.  As a convenience, you can
also just treat them like normal object methods.  These are all

    my $curl =, :followlocation,
                                 URL => '');

    $curl.setopt(:verbose, followlocation => 1);
    $curl.setopt(URL => '');


```postfields``` is actually
[CURLOPT_COPYPOSTFIELDS]( so it will always copy the fields.

Some of the normal options have **_LARGE** versions.  LibCurl always
maps the option to the **_LARGE** option where they exist, so you
don't have to worry about overflows.

These are the current options (If you want one not in this list, let
me know):


## Header Options

In addition to the normal libcurl special options that set headers
[cookie](, there
are some extra options for headers:

`Content-MD5`, `Content-Type`, `Content-Length`, `Host`, `Accept`,
`Expect`, `Transfer-Encoding`.

    $curl.Host('');  # or $curl.setopt(Host => '')
    $curl.Content-MD5('...');     # or $curl.setopt(Content-MD5 => '...')

You can also add any other headers you like:

    $curl.set-header(X-My-Header => 'something', X-something => 'foo');

You can clear a standard header by setting the header to '', or send a
header without content by setting the content to ';'

    $curl.set-header(Accept => '');      # Don't send normal Accept header
    $curl.set-header(Something => ';');  # Send empty header

If you are reusing the handle, you can also clear the set headers:


This only clears the 'extra' headers, not useragent/referer/cookie.

## Special Options

In addition to the normal libcurl options, Raku LibCurl uses options
for some special Raku functionality.

```debugfunction``` replaces the libcurl
callback, with one that looks like this:

    sub debug(LibCurl::Easy $easy, CURL-INFO-TYPE $type, Buf $buf)

    $curl.setopt(debugfunction => &debug);

```xferinfo``` replaces the libcurl
with one that looks like this:

    sub xferinfo(LibCurl::Easy $easy, $dltotal, $dlnow, $ultotal, $ulnow)

    $curl.setopt(xferinfofunction => &xferinfo);

```download``` specifies a filename to download into.

```upload``` specifies a filename to upload from.

```send``` specifies a `Str` or a `Buf` to send content from.

Finally there is a ```private``` option which replaces
CURLOPT_PRIVATE, and you can safely store any object in it.

## Errors

In most circumstances, errors from libcurl functions will result in a
thrown X::LibCurl exception.  You can catch these with CATCH.  You can
see the string error, or cast to Int to see the [libcurl error

For HTTP transfers, you can access the response code with
`getinfo('response-code')` or just `.response-code`.  You can also
check that the response code is in the 2xx range with `.success`.

You might find the
option useful to force an error if the HTTP code is equal to or larger
than 400.  That will cause an exception in those cases.

On an error, you may find [extra human readable error
with the ```.error``` method.


    CATCH {
        say "Caught!";
        when X::LibCurl {
            say "$_.Int() : $_";
            say $curl.response-code;
            say $curl.error;

## Info

After a transfer, you can retrieve internal information about the curl
session with the

You can explicitly request a single field, or a list of fields to get
a hash, or just get all the fields as a hash.  As in the options,
there are also convenience methods for each info field.

    say $curl.getinfo('effective-url');
    say $curl.getinfo('response-code');

    say $curl.getinfo(<effective-url response-code>);  # Hash with those keys

    say $curl.getinfo;   # Hash of all info fields

    say $curl.effective-url;
    say $curl.response-code;

Fields currently defined are:


## Received header fields

You can retrieve the header fields in several ways as well.

    say $curl.receiveheaders<Content-Length>;  # Hash of all headers

    say $curl.get-header('Content-Length');

    say $curl.Content-Length;

## Content

If you did not specify the ```download``` option to download content
into a file, the content will be stashed in memory in a Buf object you
can access with the `.buf()` method.

If you understand that the content is decodable as a string, you can
call the ```.content($encoding = 'utf-8')``` method which will decode
the content into a `Str`, by default with the **utf-8** encoding if
not specified.

    say "Got content", $curl.content;

## Multi-part forms

Forms now uses the libcurl MIME capability, but requires verison 7.56
or greater for forms.

There is a special POST option for multipart/formdata.

    my $curl = => 'http://...');

    # normal field
    $curl.formadd(name => 'fieldname', contents => 'something');

    # Use a file as content, but not as a file upload
    $curl.formadd(name => 'fieldname', filecontent => 'afile.txt');
    # upload a file from disk, give optional filename or contenttype
    $curl.formadd(name => 'fieldname', file => 'afile.txt',
                  filename => '',
                  contenttype => 'image/jpeg');

    # Send a Blob of contents, but as a file with a filename
    $curl.formadd(name => 'fieldname', filename => '',
                  contents => "something".encode);


This will automatically cause LibCurl to POST the data.

## Proxies

[libcurl]( has great proxy support, and
you should be able to specify anything needed as options to LibCurl to
use them.  The easiest for most common cases is to just set the
[proxy]( option.

By default, libcurl will also respect the environment variables
**http_proxy**, **ftp_proxy**, **all_proxy**, etc. if any of those are
set.  Setting the `proxy` string to `""` (an empty string) will
explicitly disable the use of a proxy, even if there is an environment
variable set for it.

A proxy host string can also include protocol scheme (`http://`) and
embedded user + password.

## Unix sockets

`LibCurl` can be used to communicate with a unix socket by setting the
`unix-socket-path` option.  You must still specify a host, but it is
ignored.  For example, you could use the [docker REST
API]( like this:

    use LibCurl::Easy;
    use JSON::Fast;

    my $docker = => '/var/run/docker.sock');

    my $info = from-json($docker.URL("http://localhost/info").perform.content);
    say $info<KernelVersion>;
    say $info<OperatingSystem>;

## Streaming

There is an experimental stream capability which can bind a Channel to
the data stream.  Instead of retrieving the buffered data at once,
each time data comes in it is sent through to the Channel.  This is
useful for long-lived connections that periodically send more data.

You can access it with Something like this:

my $curl = => ...);

my $stream = $;

start react whenever $stream -> $in {
    say "in: ", $in;


Suggestions for improving the interface for this capability welcome!

## Multi

Raku LibCurl also supports the libcurl
[multi]( interface.
You still have to construct `LibCurl::Easy` (or `LibCurl::HTTP`)
handles for each transfer, but instead of calling `.perform`, just add
the handle to a `LibCurl::Multi`.

    use LibCurl::Easy;
    use LibCurl::Multi;

    my $curl1 =, :followlocation,
                         URL => '',
                         download => './myfile1.html');

    my $curl2 =, :followlocation,
                         URL => '',
                         download => './myfile2.html');

    my $multi =;



    say $curl1.statusline;
    say $curl2.statusline;

You can also use an asynchronous callback to get a notification when
an individual transfer has completed.  The callback takes place in the
same thread with all the transfers, so it should complete quickly (or
start a new thread for heavy lifting as needed).  You can add
additional handles to the `LibCurl::Multi` at any time, even re-using
completed LibCurl::Easy handles (after setting `URL`, etc. as needed).

    use LibCurl::Easy;
    use LibCurl::Multi;

    my $curl1 =,
                                  URL => '',
                                  download => 'myfile1.html');

    my $curl2 =,
                                  URL => '',
                                  download => 'myfile2.html');

    sub callback(LibCurl::Easy $easy, Exception $e)
        die $e if $e;
        say $easy.response-code;
        say $easy.statusline;

    my $multi = => &callback);

    $multi.add-handle($curl1, $curl2);



`LibCurl` depends on [libcurl](, so
you must install that prior to installing this module.  It may already
be installed on your system.

* For debian or ubuntu: `apt-get install libcurl4-openssl-dev`
* For alpine: `apk add libcurl`
* For CentOS: `yum install libcurl`


There is another Raku interface to libcurl
[Net::Curl]( developed by
Ahmad M. Zawawi.  If you already use it and it works well for you,
great, keep using it.  Ahmad did a nice job developing it.  I would
encourage you to also take a look at this module.  LibCurl provides a
more 'rakuish' OO interface to libcurl than Net::Curl, and wraps
things in a manner to make using it a little easier (IMHO).


Copyright © 2017 United States Government as represented by the
Administrator of the National Aeronautics and Space Administration.
No copyright is claimed in the United States under Title 17,
U.S.Code. All Other Rights Reserved.

See [NASA Open Source Agreement](../master/NASA_Open_Source_Agreement_1.3%20GSC-17847.pdf) for more details.