What is curl?
curl
is a program that is included in your Mac OSX distribution used for transferring data from or to a server. It uses many supported protocols (HTTP, HTTPS, FTP, SMTP and more).
Do you have curl?
Find the location of the curl
program (executable) with $ which curl
. This will return /usr/bin/curl
if you’re on Mac.
How do I use curl?
A simple http GET request can be made like this:
$ curl google.com
How to deal with 301 redirect
Notice that you get a 301 message that is address has been “moved”. You can add the flag -L, --location
(choose either) to “follow” redirects
$ curl -L google.com
$ curl --location google.com
How do I use curl to get the weather?
There is a service called wttr
that is set up to return weather reports when you curl their service. This time we will use http for added security.
For a full local weather report:
$ curl https://wttr.in
Should I use http?
For todays weather report:
$ curl https://wttr.in?0
You can also specify a city or ski resort.
For todays weather report for New York:
curl https://wttr.in/~NewYork?0
Check if its snowy in your favorite ski resort – Charlotte Pass, Snowy Monaro, NSW, Australia
* Notice that wttr
is returning special terminal characters to change the color. You can save these to examine this in your text editor.
Congrats! You got the weather!
Note that you can curl any web service, even ones you build yourself.
Want to build your own weather service? We’ll do that next in the Part II of this series.
Bonus Section:
Can I curl my own websites?
Yes! If you followed our github pages tutorial you’ll find you can curl
your own website AND YOUR STATIC FILES
$ curl -L theptrk.github.io
<- website
$ curl -L theptrk.github.io/api/message.txt
<- static txt file
*Update – this post got onto the hackernews front page and since then the API for wttr.in has been down. **Update, the API is back up!
Couple nits to pick:
1. The URL should be https://wttr.in – HTTPS is useful for anything that crosses the web.
2. Some of the characters it writes are definitely Unicode, not ASCII.
Thanks Trish, I’ll update this
Couple nits to pick:
1. The URL should be https://wttr.in – HTTPS is useful for anything that crosses the web.
2. Some of the characters it writes are definitely Unicode, not ASCII.
Thanks Trish, I’ll update this
We broke it 🙁
$ curl wttr.in
ERROR
Oh no, this is the first thing i’ve posted to hackernews and I think that pushed it over the limit. I should apologize to igor_chubin on twitter.
We broke it 🙁
$ curl wttr.in
ERROR
Oh no, this is the first thing i’ve posted to hackernews and I think that pushed it over the limit. I should apologize to igor_chubin on twitter.
LOVE it!
Any idea how it get’s my location?
I believe it uses your IP address with a service similar to this https://www.iplocation.net/
LOVE it!
Any idea how it get’s my location?
I believe it uses your IP address with a service similar to this https://www.iplocation.net/