andrewlocatelliwoodcock

Thoughts on Software

Connecting to Cloudant from Erlang: a quick example of using HTTPS from httpc:request

with 2 comments


Wiser heads than me will no doubt already know this but I for one struggled with working out how to do this so I thought I’d put the example up in the hope it will help others. So, what I was trying to do was get and put to a Cloudant database from Erlang. Cloudant requires both HTTPS and a username and password and I struggled to find an Erlang example online.

First, make sure that you can connect to Cloudant over curl and fetch at least the _all_dbs resource. If you can’t, Erlang isn’t going to work either. I found that to get curl working correctly, I needed to update the ca cert bundle that came with it.

Here’s how to retrive _all_dbs for your Cloudant account using curl:

curl https://username:password@username.cloudant.com/_all_dbs

where username is your Cloudant username and password is the password associated with your username. For example, if your username were ‘foo’ and your password ‘bar’, here’s the command you would use:

curl https://foo:bar@foo.cloudant.com/_all_dbs

If this doesn’t work, the most likely explanation is that your ca cert bundle is out of date: follow the instructions on the curl website for how to update them.
Assuming this worked, your now in a position to try the same with Erlang. I’m going to use the httpc library here:

inets:start(),

ssl:start(),

httpc:request (get, {“https://foo:bar@foo.cloudant.com/_all_dbs”, []}, [{ssl,[{verify,0}]}], []).

It is important to remember to start inets and ssl before attempting to use httpc:request. A put to Cloudant is similar, here’s an example that assumes the existance of a database barfoo and a resource widget on your Cloudant instance and also that you have a suitably initialized variable Update which contains the data which you are writing to the widget resource:

inets:start(),

ssl:start(),

httpc:request (put, {“https://foo:bar@foo.cloudant.com/barfoo/widget”, [], [], Update}, [], []).

And there you have it

About these ads

Written by andrewlocatelliwoodcock

June 12, 2012 at 11:40

2 Responses

Subscribe to comments with RSS.

  1. Reblogged this on lava kafle kathmandu nepal.

    lkafle

    June 12, 2012 at 11:58

  2. Re curl & certs, use the -k option to ignore the cert validation chain if you’re lazy.

    Dave Cottlehuber (@dch__)

    July 11, 2012 at 07:52


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 3,430 other followers

%d bloggers like this: