Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CLOUDANT WITH POSTMAN

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.

__ a.Select the POST method from the METHOD menu,

__ 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:

-Key: grant_type and value: urn:ibm:params:oauth:grant-type:apikey


-Key: response_type and value: cloud_iam
-Key: apikey, and the value for this field should include your Cloudant instance API key, which you can
retrieve from the cloudant_credentials.json file. This file includes the Cloudant credentials, and you
already saved it in your workstation in a previous step.

__ 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.

a.Update the request URL to $URL/$DATABASE

b.Update the HTTP method to POST.

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)

d.Enter the following code as the content of the document:


{
"_id": "novel_004",
"name": "The Merry Adventures of Robin Hood",
"author": "Howard Pyle",
"year": 1883
}

e.Click Send.
f.Check the response that shows the _id and _rev fields to verify the creation of the document

To read the data of a specific document, send a GET request to $URL/$DATABASE/$DOCUMENT_ID

a.Update the request URL to $URL/$DATABASE/$DOCUMENT_ID.


b.Update $DOCUMENT_ID with the ID of the document of the “Oliver Twist” novel, which is novel_005.
c.Update the HTTP method to GET.
d.Click Send.
e.Check the response that is shown in the following figure and copy the _rev value
because it is required for updating the document in the next step.

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

a.Keep the request URL as $URL/$DATABASE/novel_005.


b.Update the HTTP method to PUT.

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)

16.To delete a document, issue a DELETE request for $URL/$DATABASE/$DOCUMENT_ID?rev=$REV

a.Update the request URL to $URL/$DATABASE/$DOCUMENT_ID?rev=$REV.


b.Update $DOCUMENT_ID with the ID of the document of the “Ivanhoe” novel, which is novel_006.
c.Update $REV with the _rev value that you copied in the previous step
d.Update the HTTP method to DELETE.
e.Click Send.
f.Check the response in the following figure to verify the deletion of the document.

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:

a.Update the request URL to $URL/$DATABASE/_index.


b.Update the HTTP method to POST
c.Update the request body with raw and JSON the following code, as shown in the following figure:
{
"index": {
"fields": [
"year"
]
},
"name": "year-json-index",
"type": "json"
}

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:

a.Update the Request URL to $URL/$DATABASE/_find.


b.Keep the HTTP Method as POST.
c.Update the Request body with the Cloudant Query content:

○“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.

You might also like