# While we can, technically, use ‹nc› to talk to HTTP servers, it's # not the most convenient thing to do. We will look at another # utility, called ‹curl› (the project name is cURL, URL being short # for ‘uniform resource locator’), which can, among other things, # fetch data from HTTP servers. Unlike ‹nc›, it will construct # headers, parse replies and do a whole bunch of other useful # things, like SSL or POST request with relative ease. # # Please note that ‹curl› is not standard (but can be installed on # most Unix systems). # We can start by a simple ‹curl› invocation to fetch a document # (the ‹-s› option tells ‹curl› to not print progress information to # ‹stderr›): echo fetch http://ix.io/1FWS curl -s http://ix.io/1FWS echo # Out of the box, curl prints the document to ‹stdout› -- this is # convenient if we want to process the output immediately using some # shell magic. But if we prefer to have it stored in a file, we can # use ‹curl -O›, which will select a filename for us: echo fetch https://www.fi.muni.cz/index.html test -f index.html && exit 1 # die if the file is already here curl -s -O https://www.fi.muni.cz/index.html test -f index.html || exit 1 # die if the file did not materialize ls -hl index.html rm index.html echo # The last option to ‹curl› (related to ‹GET› requests) to look at # is ‹-L›, for ‘location‘. In some instances, the HTTP server may # return a code like 301 which tells the client that they should be # looking elsewhere. For instance: echo fetch http://www.fi.muni.cz curl -s http://www.fi.muni.cz echo # In this case, we get a 301 Moved Permanently, with the instruction # to look at the encrypted version of the site (i.e. # ‹https://www.fi.muni.cz›). Since we don't want to handle such # matters ourselves, we can use curl -L which will automatically # follow the redirect: echo "fetch http://www.fi.muni.cz (with redirects)" curl -s -L http://www.fi.muni.cz | wc -c # That's for GET requests with cURL, let's go on to ‹curl-post.sh›.