Cloudant With Postman Practical
Cloudant With Postman Practical
Cloudant document::
{
"_id": "novel_004",
"name": "The Merry Adventures of Robin Hood",
"author": "Howard Pyle",
"year": 1883
},
{
"_id": "novel_005",
"name": "The Merry Adventures of Robin Hood",
"author": "Howard Pyle",
"year": 1885
}
You use the Cloudant API key to get a time-limited access token that is used in all the upcoming requests
to authenticate and authorize your access to Cloudant. After this token expires, you cannot use it
anymore. To retrieve this access token, complete the following steps.
__ b.Enter the following request URL into the POST field, as shown in the following figure:
https://iam.cloud.ibm.com/identity/token
__ c.Select the Body tab from the bar below the Post menu URL field, as shown in the following figure.
__ d.Select x-www-form-urlencoded and then add the following keys and values, as shown in the
following figure:
__ e.Click Send, and you receive a JSON object response that includes the access token, which is valid for
60 minutes, as shown in the following figure.
__ f.Copy the value of the access_token because it is used in all the upcoming requests.
To view all the documents in a database, issue a GET request to the following URL:
$URL/$DATABASE/_all_docs?include_docs=true
So, to show all the documents in the novels database, complete the following steps:
a.Open a new tab in Postman by clicking + in the top bar, as shown in the following figure.
b.Set the request method to the GET method.
c.Set the request URL to $URL/$DATABASE/_all_docs?include_docs=true
d.Select the Headers tab and add the following key and value
Key: Authorization.
Value: Bearer $ACCESS_TOKEN
e.Click Send. All three documents are displayed, as shown in the following figure.
To create a document, send a POST request to $URL/$DATABASE with the document's JSON content
in the request body.
c.To specify the content of the document that you want to create, click the Body tab from the tabs bar
below the request URL,
select the raw radio button, and then select the type as JSON (application/json)
e.Click Send.
f.Check the response that shows the _id and _rev fields to verify the creation of the document
14.To update a document, send a PUT request to $URL/$DATABASE/$DOCUMENT_ID with the updated
document JSON content and the latest _rev field. So, to update the year in the document of the “Oliver
Twist” novel
c.Update the request body with the following code, as shown in the following figure. Replace $REV with
the _rev value that you copied in the previous step.
{
"_rev": "$REV",
"name": "Oliver Twist",
change any field value
}
d.Click Send. In the response, notice that the value of _rev is updated (Send response)
In the next steps, you query the documents of the novels that are published after the year 1880 and
sort them by year in ascending order. To achieve this query, create an index for the year field that you
apply to the query in the next step.
To create an index, send a POST request to $URL/$DATABASE/_index with a body that includes the
fields to be indexed.
So, to create an index for the year field, complete the following steps, as shown in the following figure:
d.Click Send.
e.Check the response that is shown in the following figure and verify that the index is created
successfully.
To query a document, issue a POST request to $URL/$DATABASE/_find with a selector in the body.
A selector is a JSON object that describes the criteria that is used to select documents. So,
to query the documents of the novels that are published after the year 1880 and sort them by year in
ascending order,
complete the following steps:
○“selector” specifies querying all documents with a year greater than 1880.
○“fields” specifies that _id, name, author, and year should be returned in the query results.
○“sort” specifies to sort by year. To sort by any other field, an index should be created for the other
field.
{
"selector": {
"year": {
"$gt": 1880
}
},
"fields": [
"_id",
"name",
"author",
"year"
],
"sort": [
{
"year": "asc"
}
]
}
d.Click Send.
The query results are returned in the response that is shown in the following figure. It is a good practice
to create an index for each field that you are searching for in the selector to optimize query
performance.