Command line tools are always welcome. Today i need to setup my MAC to work with IBM Cloudant
Install curl on your MAC if you haven’t done it yet.
1 – You need to setup the Cloudant DB using your IBM Cloud account.
2 – Go to service credentials and get your username, password and hostname
Sample:
"username": "xxxx-xxx-xxx-bluemix",
"password": "12345678901234567890",
"host": "xxxx-xxx-xxx-bluemix.cloudant.com"
3 – Test the connection
curl –v –u xxxx-xxx-xxx-bluemix 'https://xxxx-xxx-xxx-bluemix.cloudant.com'
The command will prompt for the password: 12345678901234567890
You will see a lot off lines but pay attention on the line Authorization: Basic. This will be used to setup acurl (authorized curl) to avoid type the password every time you use curl.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Server auth using Basic with user ‘xxxx-xxx-xxx
-bluemix’
* Using Stream ID: 1 (easy handle 0x7fabd0805800)
> GET / HTTP/2
> Host: xxxx-xxx-xxx
-bluemix.cloudant.com
> Authorization: Basic asddadasdDASDdaDadaDSERQERQRQERQEDSGSFDGSFDG==
4 – Edit the .bash_profile or .bashrc file in any text editor. For example, from the command line, issue the following command:
open –e .bash_profile
5 – Add the following line to the file, then save the file.
alias acurl="curl -s --proto '=https' -g -H 'Authorization: Basic asddadasdDASDdaDadaDSERQERQRQERQEDSGSFDGSFDG=='"
6 – On the terminal run : source .bash_profile
7 – Issue a command using the new alias. For example, issue a command to view your account information.
acurl -X GET 'https://xxxx-xxx-xxx-bluemix.cloudant.com'
TIP: The hostname is not friendly you can edit your hosts file and create an alias:
On MAC terminal type: sudo vi /etc/hosts
ping your hostname to get the ip address
add a line like this one bellow:
<ip address> xxxx-xxx-xxx -bluemix.cloudant.com keniodb.cloudant.com
Save the hosts file
test the connection
acurl -X GET 'https://keniodb.cloudant.com