Download File With Curl Mac



  1. Curl Download Multiple Files
  2. Download File With Curl Macros
  3. Windows Curl Download File
  4. Curl Download File To Location
  5. Use Curl To Download A File
  • This message:[ Message body ] [ More options ]
  • Related messages:[ Next message ][ Previous message ][ In reply to ][ Next in thread ] [ Replies ]
From: Nick Zitzmann <nick_at_chronosnet.com>
Date: Mon, 11 Feb 2013 12:51:13 -0700

In fact, curl is more than just a command line utility for macOS, Linux or Windows. This is a set of libraries that implement the basic capabilities of working with URL pages and file transfer. Download files using Curl Curl can be used to transfer data over a number of protocols. It supports many protocols including HTTP, HTTPS, FTP, TFTP, TELNET, SCP, etc. Using Curl, you can download any remote files. CURL Command to Download and Save File. To simply download a file using curl use following. If you set up a queue of files to download in an input file and you leave your computer running to download the files, the input file may become stuck while you're away and retry to download the content. You can specify the number of retries using the following switch. Make sure the letter O is capitalized. After you type curl -O, just paste the URL of the file you want to download. Don’t include the “” either, that’s just an insertion point. Your download will.


On Feb 11, 2013, at 9:26 AM, Jürgen Keser <jkeser_at_computerworks.de> wrote:

> Hi All,
>
> we use Mac OS X 10.8 and Xcode 4.5.2. Here we try now to create our first simple (Cocoa Application) example with the
> curl-library. We will download an file with this library and place the downloaded file to a special place. Enclosed I'll send you
> the code we use until now. The curl_easy_setopt functions return all true, but the file will not be downloaded.
> Also the Cocoa Application will cause an crash at the end. Is there anyone out there who can help me?

I noticed three problems immediately:

> fp = fopen(outfilename,'curl-7.29.0.tar.gz');

Curl Download Multiple Files

This is an illegal use of the fopen() function. Read the man page for more details, but you need to do something like this instead:
fp = fopen('/Path/To/Some/Place/Writable/curl-7.29.0.tar.gz', 'w');

> if(CURLE_OK (res = curl_easy_setopt(curl, CURLOPT_URL, url))){
> NSAlert *alert = [[[NSAlert alloc] init] autorelease];

The NSAlert class is part of the AppKit framework. If you want to use AppKit, you need to initialize it first by calling either NSApplicationMain() or NSApplicationLoad(). See Apple's documentation for more information.

Also, you are autoreleasing an object without first creating an autorelease pool. Unless your tool is using GC (which is deprecated), you must first create an NSAutoreleasePool or use the @autoreleasepool directive before calling -autorelease.

> This message is intended for the addressee only. It contains private and confidential information. The contents are not to be disclosed to anyone other than the addressee. Unauthorized recipients are requested to comply with the above and to inform the sender immediately of any errors in transmission by replying to this message and please delete it from your computer.

I sure hope I'm an authorized recipient… :)

Nick Zitzmann
<http://www.chronosnet.com/>

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-02-11

  • This message: [ Message body ]
  • Next message: Steve Holme: 'RE: Mem leak in curl_ntlm_msg.c'
  • Previous message: Jürgen Keser: 'download an file on Mac OS X'
  • In reply to: Jürgen Keser: 'download an file on Mac OS X'
  • Next in thread: Jürgen Keser: 'Fwd: download an file on Mac OS X'
  • Reply: Jürgen Keser: 'Fwd: download an file on Mac OS X'
  • Contemporary messages sorted: [ by date ] [ by thread ] [ by subject ] [ by author ] [ by messages with attachments ]

The curl tool lets us fetch a given URL from the command-line. Sometimes we want to save a web file to our own computer. Other times we might pipe it directly into another program. Either way, curl has us covered.

See its documentation here.

This is the basic usage of curl:

Download File With Curl Macros

That --output flag denotes the filename (some.file) of the downloaded URL (http://some.url)

Let's try it with a basic website address:

Besides the display of a progress indicator (which I explain below), you don't have much indication of what curl actually downloaded. So let's confirm that a file named my.file was actually downloaded.

Using the ls command will show the contents of the directory:

Which outputs:

And if you use cat to output the contents of my.file, like so:

– you will the HTML that powers http://example.com

I thought Unix was supposed to be quiet?

Let's back up a bit: when you first ran the curl command, you might have seen a quick blip of a progress indicator:

If you remember the Basics of the Unix Philosophy, one of the tenets is:

Rule of Silence: When a program has nothing surprising to say, it should say nothing.

In the example of curl, the author apparently believes that it's important to tell the user the progress of the download. For a very small file, that status display is not terribly helpful. Let's try it with a bigger file (this is the baby names file from the Social Security Administration) to see how the progress indicator animates:

Quick note: If you're new to the command-line, you're probably used to commands executing every time you hit Enter. In this case, the command is so long (because of the URL) that I broke it down into two lines with the use of the backslash, i.e.

This is solely to make it easier for you to read. As far as the computer cares, it just joins the two lines together as if that backslash weren't there and runs it as one command.

Make curl silent

The curl progress indicator is a nice affordance, but let's just see if we get curl to act like all of our Unix tools. In curl's documentation of options, there is an option for silence:

-s, --silent

Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. It will still output the data you ask for, potentially even to the terminal/stdout unless you redirect it.

Try it out:

Repeat and break things

So those are the basics for the curl command. There are many, many more options, but for now, we know how to use curl to do something that is actually quite powerful: fetch a file, anywhere on the Internet, from the simple confines of our command-line.

Before we go further, though, let's look at the various ways this simple command can be re-written and, more crucially, screwed up:

Shortened options

As you might have noticed in the --silent documentation, it lists the alternative form of -s. Many options for many tools have a shortened alias. In fact, --output can be shortened to -o

Now watch out: the number of hyphens is not something you can mess up on; the following commands would cause an error or other unexpected behavior:

Also, mind the position of my.file, which can be thought of as the argument to the -ooption. The argumentmust follow after the -o…because curl.

If you instead executed this:

How would curl know that my.file, and not -s is the argument, i.e. what you want to name the content of the downloaded URL?

In fact, you might see that you've created a file named -s…which is not the end of the world, but not something you want to happen unwittingly.

Order of options

By and large (from what I can think of at the top of my head), the order of the options doesn't matter:

In fact, the URL, http://example.com, can be placed anywhere in the mix:

File

A couple of things to note:

  1. The way that the URL, what you might consider the main argument for the curl command, can be placed anywhere after the command is not the way that all commands have been designed. So it always pays to read the documentation with every new command.
  2. Notice how -s http://example.com doesn't cause a problem. That's because the -s option doesn't take an argument. But try the following:

And you will have a problem.

No options at all

The last thing to consider is what happens when you just curl for a URL with no options (which, after all, should be optional). Before you try it, think about another part of the Unix philosophy:

This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.

If you curl without any options except for the URL, the content of the URL (whether it's a webpage, or a binary file, such as an image or a zip file) will be printed out to screen. Try it:

Output:

Even with the small amount of HTML code that makes up the http://example.com webpage, it's too much for human eyes to process (and reading raw HTML wasn't meant for humans).

Standard output and connecting programs

But what if we wanted to send the contents of a web file to another program? Maybe to wc, which is used to count words and lines? Then we can use the powerful Unix feature of pipes. In this example, I'm using curl's silent option so that only the output of wc (and not the progress indicator) is seen. Also, I'm using the -l option for wc to just get the number of lines in the HTML for example.com:

Number of lines in example.com is: 50

Windows Curl Download File

Now, you could've also done the same in two lines:

Curl Download File To Location

Download File With Curl Mac

Use Curl To Download A File

But not only is that less elegant, it also requires creating a new file called temp.file. Now, this is a trivial concern, but someday, you may work with systems and data flows in which temporarily saving a file is not an available luxury (think of massive files).