Lab - Explore Rest Apis With Api Simulator and Postman
Lab - Explore Rest Apis With Api Simulator and Postman
NIM : J3D119029
Kelas : TEK 3B P1
Background / Scenario
The DEVASC VM includes a School Library API simulator with API documentation and an associated
database. You can use the simulator offline to explore APIs and test their functionality.
In this lab, you will learn how to use the School Library API simulator to make API calls to list, add, and delete
books. Later, you will use Postman to make these same API calls.
Required Resources
1 PC with operating system of your choice
Virtual Box or VMWare
DEVASC Virtual Machine
Instructions
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
Response content type - Click application/json to see a list of the different types of data
formats the information can be viewed. Leave the selection as application/json.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
Code - The code displays 200 by default, which indicates the API request from the sever was a
success as displayed in the Description. (You have not sent an API request yet.)
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
Open a terminal window. Right-click and Paste the contents from the clipboard into the terminal and
press Enter. Notice this provides the same information as the library's OpenAPI interface.
devasc@labvm:~$ curl -X GET "http://library.demo.local/api/v1/books" -H
"accept: application/json"
[
{
"id": 0,
"title": "IP Routing Fundamentals",
"author": "Mark A. Sportack"
},
{
"id": 1,
"title": "Python for Dummies",
"author": "Stef Maruch Aahz Maruch"
},
{
"id": 2,
"title": "Linux for Networkers",
"author": "Cisco Systems Inc."
},
{
"id": 3,
"title": "NetAcad: 20 Years Of Online-Learning",
"author": "Cisco Systems Inc."
}
]
devasc@labvm:~$
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
Step 7: List books with their ISBN using the GET /books API.
a. Return to the School Library API web site's GET /books API.
b. In the Parameters section, select the down arrow next to the includeISBN parameter and select true.
c.
d. Click Execute.
Notice the following changes in Responses:
Curl now includes the parameter for ISBN.
curl -X GET "http://library.demo.local/api/v1/books?includeISBN=true" -H
"accept: application/json"
Response body has the same list of books as shown previously but now includes the book's
ISBN.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
To minimize the scrolling, when you are done with an API you can close that specific API window by
clicking anywhere on the title bar. Now you can see all the APIs more easily.
d. The token will be displayed in the Response body. Select the information between the quotes, right-click
and Copy the information into your clipboard. Your token will differ from the one shown below.
{
"token": "cisco|KZZzteQbC5iV3HKEzB7hCJ6qHQXen4rLGh72YJKeVfs"
}
e. Scroll up to the top of the School Library API page and click the green Authorize button. The Available
authorizations dialogue box will appear.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
f. Right-click and Paste the token after Value and click Authorize. Notice the Name is X-API-KEY. This
information along with the token will be used later in Postman.
g. Close the Available authorizations dialogue box and return to the list of APIs. Notice the locks by
several of the APIs have now changed. These APIs are now available for you to use.
h. Click the bar for the API POST /loginViaBasic to close the window.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
e. Click Execute.
f. Verify that the post was successful in the Server response. A Code of 200 means the post was a
success. You should see the book you added in the Response body along with a new id. You will also
see updated information for curl and the Request URL.
g. To add another book, modify the id, title and author with the information shown below.
{
"id": 5,
"title": "31 Days Before Your CCNA Exam",
"author": "Allan Johnson"
}
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
h. Click Execute.
i. Verify that the post was successful in the Server response. A Code of 200 means the post was a
success. You should see the book you added in the Response body along with a new id. You will also
see updated information for curl and the Request URL.
Note: If you got a 401 code, check the Response body text. Most likely you received an "error":
"Invalid API key" response. This is because you did not enter all the characters for your API key. Or
possibly, you add an unnecessary space. Return to the previous step and repeat the authorization
process.
j. Click the bar for the API POST /books to close the window.
k. You can verify the books were added to the Our Books page. Return to the School Library tab in your
browser (http://library.demo.local) and refresh the page. Be careful not to close the School Library API
tab. If you do, then you will need to reauthenticate.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
{
"id": 0,
"title": "IP Routing Fundamentals",
"author": "Mark A. Sportack"
},
{
"id": 1,
"title": "Python for Dummies",
"author": "Stef Maruch Aahz Maruch"
},
{
"id": 2,
"title": "Linux for Networkers",
"author": "Cisco Systems Inc."
},
{
"id": 3,
"title": "NetAcad: 20 Years Of Online-Learning",
"author": "Cisco Systems Inc."
},
{
"id": 4,
"title": "IPv6 Fundamentals",
"author": "Rick Graziani"
},
{
"id": 5,
"title": "31 Days Before Your CCNA Exam",
"author": "Allan Johnson"
}
]
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
e. Click the bar for the GET /books API to close the window.
Step 11: List a specific book using the GET /books{id} API.
a. Click the GET /books{id} API. Notice this API requires the id as a parameter.
b. To the right of Parameters, click the Try it out button.
c. Under Parameters, enter 4 for the required id.
d. Click Execute. Notice the information provided by Curl and Request URL.
Curl - This is the curl command to perform the same function using curl.
Request URL - This is the URL that can be used to get the same information using Postman and
Python.
Verify that the get was successful in the Server response. A Code of 200 means the post was a
success. In the Response body you will see the book you requested with the id of 4.
{
"id": 4,
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
e. Click the bar for the GET /books{id} API to close the window.
Step 12: Delete a specific book using the DELETE /books{id} API.
a. Click the DELETE /books{id} API. Notice this API requires the id as a parameter.
b. Click Try it out.
c. Under Parameters, enter 4.
d. Click Execute.
e. Verify that the delete was successful in the Server response. A Code of 200 means the post was a
success. In the Response body you will see the book you deleted with the id of 4.
{
"id": 4,
"title": "IPv6 Fundamentals",
"author": "Rick Graziani"
}
f. Click the bar for the DELETE /books{id} API to close the window.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 13 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
b. Click Try it out. If you see Cancel button in red, then you are already in Try it out mode.
c. Click Execute.
d. Under Server response in the Response body, you will no longer see the book with id or 4.
[
{
"id": 0,
"title": "IP Routing Fundamentals",
"author": "Mark A. Sportack"
},
{
"id": 1,
"title": "Python for Dummies",
"author": "Stef Maruch Aahz Maruch"
},
{
"id": 2,
"title": "Linux for Networkers",
"author": "Cisco Systems Inc."
},
{
"id": 3,
"title": "NetAcad: 20 Years Of Online-Learning",
"author": "Cisco Systems Inc."
},
{
"id": 5,
"title": "31 Days Before Your CCNA Exam",
"author": "Allan Johnson"
}
]
Note: Do not close the School Library API tab in the Chromium browser. You will use the API
documentation in the next part.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 15 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
Note: You can save the JSON output to a file using the Save Response button above the output. This is
not required for this lab.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 16 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
e. Click Send.
f. If necessary, scroll down to the Body section to see your new token. Your token will be different than
the one shown here.
{
"token": "cisco|5xSUHYFDvIAoCRv0LqWVSDcjJAwWjg18vMml6u2lm1I"
}
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 17 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
Note: Recall that you saw X-API-KEY in the School Library API web page when you got a token
selecting the green Authorize button.
3) Return to the Post tab in Postman and copy the token you received in Step 3. Be sure to include
everything within the quotation marks. Your token will be different than the one shown here.
Example: cisco|5xSUHYFDvIAoCRv0LqWVSDcjJAwWjg18vMml6u2lm1I
4) Go back to the second Post tab in Postman. Paste the token in the Value field
e. In the same row with the Authorization tab, click Body. This section will allow you to choose the format
of your input.
Click the raw radio button.
Click Text and change this option to JSON.
f. In the input area you will see the number 1, for "line 1". Enter the following JSON object.
{
"id": 4,
"title": "IPv6 Fundamentals",
"author": "Rick Graziani",
"isbn": "978 158144778"
}
g. Click Send.
h. To verify that the API request was a success, you will now see a response that include the Status code
200 OK in green.
Step 5: Verify the additional book with the Get /books API.
a. Return to the first GET tab. As you can see, Postman makes it easy to switch between different API calls.
b. Click Send.
c. To verify that the API request was a success, you will now see a response that include the Status code
200 OK in green.
d. Click Body to see the response. Notice that the default is Pretty and json.
[
{
"id": 0,
"title": "IP Routing Fundamentals",
"author": "Mark A. Sportack"
},
{
"id": 1,
"title": "Python for Dummies",
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 18 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 19 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
Notice that the Request URL now includes the parameters. You will see this again in Postman.
http://library.demo.local/api/v1/books?includeISBN=true&sortBy=author
f. Return to Postman and go to the first API tab, GET http://library.demo.local/api/v1/books. You will now
include some of the parameters from the School Library API web site.
g. Click Params. You will see under Query Params input boxes for KEY and VALUE. Enter the following
information:
Under KEY, enter includeISBN and under Value enter true
Notice a check mark will automatically be included to the left of the value and a new row added.
Under KEY, enter sortBy and under Value enter author
Notice that when entering these query parameters, it has updated the original URL next to the GET. This
is the same Request URL you saw in the School Library API web site for this same API call. This is the
URL Postman will be using, with these query parameters when making the API call.
http://library.demo.local/api/v1/books?includeISBN=true&sortBy=author
h. Click Send.
Notice in the Body, it now shows the same list of books, sorted by author and including the ISBNs that
you saw in the School Library API web site.
[
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 20 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
{
"id": 5,
"title": "31 Days Before Your CCNA Exam",
"author": "Allan Johnson"
},
{
"id": 2,
"title": "Linux for Networkers",
"author": "Cisco Systems Inc.",
"isbn": "000-0000000123"
},
{
"id": 3,
"title": "NetAcad: 20 Years Of Online-Learning",
"author": "Cisco Systems Inc.",
"isbn": "000-0000001123"
},
{
"id": 0,
"title": "IP Routing Fundamentals",
"author": "Mark A. Sportack",
"isbn": "978-1578700714"
},
{
"id": 4,
"title": "IPv6 Fundamentals",
"author": "Rick Graziani",
"isbn": "978 1587144778"
},
{
"id": 1,
"title": "Python for Dummies",
"author": "Stef Maruch Aahz Maruch",
"isbn": "978-0471778646"
}
]
Step 1: Open Visual Studio (VS) Code and navigate to the school-library directory.
a. Open VS Code from the Menu button or by double-clicking the icon on the desktop.
b. Click File > Open Folder..., navigate to the labs/devnet-src/school-library folder, and click OK.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 21 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
b. At the top, notice the “shebang” that sets the interpreter to Python 3 and then the three libraries that are
imported.
#!/usr/bin/env python3
import requests
import json
from faker import Faker
c. You will use the Python requests library throughout this course. The requests library is required if you
want to use Python to make API requests using GET, POST, DELETE and other HTTP methods.
d. Faker is a Python library that generates 'fake' data for you. This program uses the Python faker library to
generate random book titles, authors, and ISBNs. You can search the internet for more information on
faker library. However, complete the following steps to see all the 252 methods for the faker library.
1) Open a terminal window and start Python 3.
2) From faker import the Faker() module.
3) Assign the Faker() module to fake.
4) To see all the methods, enter fake. and then press the tab key twice. Notice the method fake.name(),
which you will use in the next step. In the next step and later in this lab, you will also use the three
highlighted methods (prefaced with fake.).: catch_phrase(), isbn13(), and name().
devasc@labvm:~/labs/devnet-src/school-library$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from faker import Faker
>>> fake = Faker()
>>> fake.<press Tab key twice – be sure you include the period>
Display all 252 possibilities? (y or n)
fake.add_provider( fake.future_datetime( fake.pyfloat(
fake.address( fake.generator_attrs fake.pyint(
fake.am_pm( fake.get_formatter( fake.pyiterable(
<output omitted>
fake.catch_phrase( fake.ipv4_public( fake.random_element(
fake.chrome( fake.isbn10( fake.random_int(
fake.city( fake.isbn13( fake.random_letter(
<output omitted>
fake.date_time_ad( fake.msisdn( fake.texts(
fake.date_time_between( fake.name( fake.time(
<output omitted>
fake.future_date( fake.pydict( fake.zipcode_plus4(
>>>
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 22 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
Question:
b. Using the three highlighted messages in Step 2d above, enter the command that would print the following
fake output.
Type your answers here.
My name is Gary Castaneda and I wrote "Organic incremental neural-net" (ISBN 978-0-
669-01935-3).
>>>
c. Later in the program, a loop is used to iterate through these three methods to create entries for the
School Library. Enter the following to generate 10 random names. After the "…" you will need to press
return a second time.
>>> for i in range(10):
... print(fake.name())
...
Kevin Moyer
Mr. Christopher Green MD
Spencer Jensen
Whitney Guzman
Nicole Scott
Tammy Lewis
Craig Edwards
Michael Diaz
Ryan Mccoy
Terry Rocha
>>>
d. Quit the Python interpreter when done investigating the faker library.
>>> quit()
devasc@labvm:~/labs/devnet-src/school-library$
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 23 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
b. If the call is unsuccessful (HTTP status code is not equal to 200), an exception is raised and printed to the
terminal window. Again, notice the use of an f-string to build the exception message. You can test the
exception code by changing the one of the variables in Step 4.
if r.status_code == 200:
return r.json()["token"]
else:
raise Exception(f"Status code {r.status_code} and text {r.text}, while trying
to Auth.")
b. If the call is unsuccessful, an exception is raised and printed to the terminal window. You can test it by
changing the one of the variables in Step 4.
if r.status_code == 200:
print(f"Book {book} added.")
else:
raise Exception(f"Error code {r.status_code} and text {r.text}, while trying
to add book {book}.")
b. The Faker() module is set to a variable named fake. A for loop then iterates 100 times. The i variable is
used later in the loop to set the value for the id key for each new book from 4 up to and not including 104.
Note: If you want to keep the two previous books added previously in this lab, change range to (6, 106).
fake = Faker()
for i in range(4, 104):
c. Next, three variables hold the value of methods invoked from the Faker() module: catch_phrase(),
name(), and isbn13().
fakeTitle = fake.catch_phrase()
fakeAuthor = fake.name()
fakeISBN = fake.isbn13()
d. Recall that the payload parameter for the books API requires JSON in the following format:
{
"id": 0,
"title": "string",
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 24 of 25 www.netacad.com
Lab - Explore Rest APIs with API Simulator and Postman
"author": "string"
}
The book variable is built using the three required keys for the payload parameter and values from the
three fake variables.
book = {"id":i, "title": fakeTitle, "author": fakeAuthor, "isbn": fakeISBN}
e. Finally, the addBook function is called passing the book and apiKey variables. Because addBook is
part of the loop, it will be called 101 times, one time each for book ID 4 through 105.
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 25 of 25 www.netacad.com