How to Use CURL to Hit a Web Api Using HTTP Basic Authentication

Curl is a fantastic application.

The most basic use of curl is very straightforward, just put in a web site’s url:

curl http://twitter.com

If you copy and paste the above code, you’ll get the HTML output of twitter’s home page.

In order to demo this, I created a twitter account. You can sign up for one on your own.

Now, to hit their api and update your status, they require you use HTTP Basic Authentication. No sweat, we can use the -u flag for that. The request must be sent as a post, so you must use the -d flag (data).

curl -u username:password' -d "status=i%20am%20human" http://twitter.com/statuses/update.json

Substitute your username and password in the appropriate place.

There you have it. The .json at the end of the url indicates what format you want your results returned in. Twitter also supports XML, RSS, and ATOM.

I’ll be posting a follow up on how to perform curl POST requests using PHP in the next few days.

The twitter documentation is very well written, check it out for more examples and other calls.

If you found this post helpful, please consider sharing to your network. I'm also available to help you be successful with your distributed systems! Please reach out if you're interested in working with me, and I'll be happy to schedule a free one-hour consultation.