Inspect the JSON from the response payload
Seeing the response from curl or Postman is cool, but how do you make use of the JSON data? With most API documentation, you don’t need to show how to make use of JSON data. You assume that developers will use their front-end development skills to parse through the data and display it appropriately in their apps. However, to better understand how developers will access the data, we’ll go through a brief tutorial to display the REST response on a web page.
- Activity: Make an API request on a web page
- The AJAX method from jQuery
- Logging responses to the console
- Inspect the payload
Activity: Make an API request on a web page
For this activity, you’ll use JavaScript to display the API response on a web page. Specifically, you’ll use some auto-generated jQuery code from Postman to create the AJAX request. You’ll get the wind speed from the response and print it to the page.
-
In an editor such as Sublime Text, create a new HTML file called
weather.html
and insert the following boilerplate code:<html> <meta charset="UTF-8"> <head> <title>Sample page</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> POSTMAN CODE GOES HERE </script> </head> <body> <h1>Sample Page</h1> </body> </html>
- Assuming you completed the exercises in the Postman tutorial to configure a request, go back into Postman.
-
In Postman, click the Code link (below the Save button) and go to JavaScript - jQuery:
Copying JavaScript code from Postman - Copy the Postman code above and insert it into the
POSTMAN CODE GOES HERE
place in yourweather.html
file. -
Directly below
console.log(response);
, add these two lines:var content = response.wind.speed; $("#windSpeed").append(content);
-
Your final code should look as follows:
<html> <meta charset="UTF-8"> <head> <title>Sample page</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> var settings = { "url": "https://api.openweathermap.org/data/2.5/weather?zip=95050&units=imperial&appid=APIKEY", "method": "GET", "timeout": 0, }; $.ajax(settings).done(function (response) { console.log(response); var content = response.wind.speed; $("#windSpeed").append(content); }); </script> </head> <body> <h1>Sample Page</h1> </body> </html>
(In the above code, replace
APIKEY
with your actual API key.)What is this code doing? In a nutshell, when
ajax
(a jQuery function) retrieves the response from the API, it assigns the response toresponse
. A variable calledcontent
is created and set it equal toresponse.wind.speed
(dot notation is used to access this value). jQuery’sappend
method insertscontent
after an element called#windSpeed
on the page. (I realize this is an extremely abbreviated explanation, but explaining JavaScript is beyond the scope of this course. In general, you can learn more by reading about the jQuery.ajax() function.) -
Start Chrome and open the JavaScript Console.
To open the JavaScript Console, on Chrome on a Mac, go to View > Developer > Javascript Console; on Windows, click the menu button (vertical ellipses) and go to More tools > Developer tools. Then click the Console tab.
-
In Chrome, press Cmd+O (Mac) or Ctrl + O (Windows) and select your
weather.html
file.The weather response should be logged to the JavaScript Console (due to the
console.log(response)
code in the request). If you expand the object returned to the console, it will look as follows:You can view the file here: weather-plain.html.
The AJAX method from jQuery
In this section, I’ll explain a bit more about the ajax
function you used earlier. This information probably isn’t essential for documenting REST APIs, but it’s good to understand. In the code, here’s the ajax
script:
<script>
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.openweathermap.org/data/2.5/weather?zip=95050&appid=APIKEY&units=imperial",
"method": "GET"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
</script>
(In the above code, replace APIKEY
with your actual API key.)
If you’re working with JavaScript and APIs, the ajax
method from jQuery can be helpful with code samples. This ajax
method takes one argument: settings
.
$.ajax(settings)
The settings
argument is an object that contains a variety of key-value pairs.
var settings = {
}