Week 9 - Networking and APIs

Introduction to Networking

Client Server Model

TCP/IP Protocol

HTTP Protocol

URL Format

Web APIs

API Testing Tools

Using APIs

Practice

Assignment

Core program

API Testing Tools

Here we will discuss the two most commonly used API testing tools:

We will be using the following API to demonstrate these tools: JSONPlaceholder - Free Fake REST API. As indicated by its name, it returns fake data, but that is good enough for our purposes here. Click on the Guide menu option on the JSONPlaceholder site to show some examples of how to use the API. The examples use JavaScript code, but for now we will only use the URLs that are passed to the fetch() function in the examples.

The curl Command

The curl command, sometimes spelled as cURL ("client URL"), is a command-line tool that developers use to transfer data to and from a server. You must run it from a terminal window. It is included in the standard installations of Windows and macOS.

The curl command is most often used to quickly GET some data from an API at a specified URL and display it in the terminal window or perhaps redirect the output to a file. The curl command also supports other HTTP methods besides GET, but then the command syntax quickly becomes unwieldy.

Here is an example of a simple GET request to the JSONPlaceholder API. Note that the GET method is used by default and therefore does not need to be specified explicitly on the command line.

curl [<https://jsonplaceholder.typicode.com/todos/1>](<https://jsonplaceholder.typicode.com/todos/1>)

Response:

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

Another curl example, now for a POST request:

curl '[<https://jsonplaceholder.typicode.com/todos>](<https://jsonplaceholder.typicode.com/todos>)'' \\
  -X POST \\
  -H 'Content-Type: application/json' \\
  -d '{"title":"Get a haircut","completed":false,"userId":1}'

Response:

{
  "title": "Get a haircut",
  "completed": false,
  "userId": 1,
  "id": 201
}

The backslash character at the end of the first three lines indicates to the terminal that the command continues on the next line. The final line is not terminated with a backslash and represents the end of the command.

<aside> ⌨️

Use curl to try some other GET requests from the JSONPlaceholder examples, for example:

Postman

The Postman API testing tool is an application that allows developers and testers to easily build, test, and document APIs. It provides a user-friendly GUI (Graphical User Interface) for interacting with APIs without needing to write complex code.

Postman Installation

To install Postman, visit https://www.postman.com/ and click the button to download the desktop app for your computer’s operating system.

When the installation starts you are prompted to create a free account. Without an account the functionality that Postman offers is very limited, so we recommend that you create one.

The picture below shows Postman’s main window.

image.png

Let’s try to issue the same GET request as used above with the curl command. Type the first URL in the input box next to the GET button and press the Send button.

image.png

In the bottom half of the screen the response will be shown (see screenshot below). The Body tab shows the response content, in this case in JSON format. We can also see that the response status code was 200 OK, which indicates a normal successful response. The response time was 105 ms and the content size is 1.3 KB.

image.png

The Headers tab shows the response headers. We can see that the response Content-Type header indicates that the response body is of media type application/json. That is how Postman decided to display the content in the Body tab as JSON.

image.png

Postman allows you to save your requests in collections. When you start Postman for the first time it comes with a default collection called “My Collection”. You can now save the GET request in this collection by pressing the Save button.

You can also create a new collection by pressing the New button and select Collection from the pop-up window. You can then enter the name that you want to assign to the collection, for instance “JSONPlaceholder”:

image.png

Let’s recreate the POST request that we used earlier with curl, try it and finally save it in the new JSONPlaceholder collection.

  1. Press the ➕ button next to the JSONPlaceholder collection tab.
  2. Click on Untitled Request to give this request a name, for example "Create a new post".
  3. Click on the dropdown of the GET button and select POST.
  4. Enter the same URL we have used with curl in the URL field: ](<https://jsonplaceholder.typicode.com/posts>)[<https://jsonplaceholder.typicode.com/posts>`.
  5. Click on Body below the URL.
  6. Make sure the raw radio button is selected and JSON as the content type.
  7. Enter the JSON request body in the text area. Make sure that you enter syntactically correct JSON.

With all this done, your screen should look like this:

image.png

Now press the Send button. If all is well you should get a response that looks like this:

image.png

The status code 201 Created indicates that the POST request was successful. The returned JSON data object includes an id property assigned by the JSONPlaceholder server for the newly created post.

You can now save this POST request in the JSONPlaceholder collection by pressing the Save button and selecting the JSONPlaceholder collection. You can recall saved requests by selecting from the collection window.


The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 **

CC BY-NC-SA 4.0 Icons

*https://hackyourfuture.net/*