Friday 6 November 2020

Backslash broke my Cloudant

Well, the headline somewhat overhypes things BUT following on from my earlier post: -

Cloudy databases - Back with Cloudant on IBM Cloud

I noted that one can get the URL of a Cloudant instance from the Manage -> Overview page: -


and copy the External Endpoint (preferred) URL.

When this URL is copied to the clipboard, it includes a trailing slash ( / ), which is totally fine.

Right ?

Right ?

Well, not necessarily.

I then go on to describe how that URL can be added to a $URL environment variable: -

export URL="https://d66573ef-c567-4efc-6e59-c8ab7eb33de6-bluemix.cloudantnosqldb.appdomain.cloud/"

and then use that for various cURL commands such as: -

curl -s -k -X GET -H 'Authorization: Bearer '"$ACCESS_TOKEN" $URL/music | json_pp

What I'd missed was.... it just does not work

That command fails with: -

{
   "reason" : "Database does not exist.",
   "error" : "not_found"
}

What was worse - I couldn't even create new databases: -

curl -s -k -X PUT -H 'Authorization: Bearer '"$ACCESS_TOKEN" $URL/chocolate | json_pp

{
   "reason" : "Database does not exist.",
   "error" : "not_found"
}

curl -v -k -X PUT -H 'Authorization: Bearer '"$ACCESS_TOKEN" $URL/cookies | json_pp

{
   "reason" : "Database does not exist.",
   "error" : "not_found"
}

which made absolutely no sense whatsoever.

It took me a while to work out what was going wrong ....

As mentioned, when I copied the URL from the Cloudant Overview page, it was coping the trailing slash.

This meant that the $URL variable included the slash ...

And then, when I specified the database name appended to the URL, I had: -

$URL/music

or: -

$URL/cookies

which, when the variable is expanded by the shell, turns into: -

https://d66573ef-c567-4efc-6e59-c8ab7eb33de6-bluemix.cloudantnosqldb.appdomain.cloud//music

or: -

https://d66573ef-c567-4efc-6e59-c8ab7eb33de6-bluemix.cloudantnosqldb.appdomain.cloud//cookies

In other words, we're trying to access / create a database called /music or /cookies.

Once I realised what was going wrong, I amended the $URL variable: -

export URL="https://d66573ef-c567-4efc-6e59-c8ab7eb33de6-bluemix.cloudantnosqldb.appdomain.cloud"

and all was well: -

curl -s -k -X PUT -H 'Authorization: Bearer '"$ACCESS_TOKEN" $URL/chocolate | json_pp

{
   "ok" : true
}

curl -s -k -X GET -H 'Authorization: Bearer '"$ACCESS_TOKEN" $URL/chocolate | json_pp

{
   "props" : {},
   "compact_running" : false,
   "update_seq" : "0-g1AAAAP3eJzLYWBgEMhgTmFQT0lKzi9KdUhJMjTQS8rVTU7WTS3VTUnVNTTWS87JL01JzCvRy0styQFqYMpjAZIMH4DUfyDISmQAmaAGN8GISAMeQAx4j2GAKZEGXIAYcB_DAAsiDTgAMeA81AAyAmEDxIT9ZAfCAogB68kOhAkQA-aTHQgNEAP6yQmEpAIgmVRPXipISgBpzsemmbDnkwJAmuOxaSbs8SQHkGZ_hGbSPG0A0m1PpqcVQJr1yfS0AEizPHmeTmRI4ofozAIACC1D2Q",
   "doc_del_count" : 0,
   "purge_seq" : 0,
   "instance_start_time" : "0",
   "doc_count" : 0,
   "sizes" : {
      "active" : 0,
      "external" : 0,
      "file" : 133940
   },
   "cluster" : {
      "w" : 2,
      "r" : 2,
      "n" : 3,
      "q" : 16
   },
   "disk_format_version" : 8,
   "db_name" : "chocolate"
}

Nice!

No comments:

Visual Studio Code - Wow 🙀

Why did I not know that I can merely hit [cmd] [p]  to bring up a search box allowing me to search my project e.g. a repo cloned from GitHub...