INFO 253A: Frontend Web Architecture
Kay Ashaolu
From Quora: An API (Application Programming Interface) is best thought of as a contract provided by one piece of computer software to another.
function getMaximum(num1, num2) {
/* ...
working code
...
*/
return answer;
}
../js/api.js
function getMaximum(num1, num2) {
let answer = num1;
if (num1 < num2) {
answer = num2;
}
return answer;
}
index.html
<!DOCTYPE html>
<html>
<body>
<button onclick="alert(getMaximum(5, 3))">
Call API function getMaximum
</button>
<script src="../js/api.js"></script>
</body>
</html>
{
"coord": {
"weather": [
{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01d"
}
],
"base": "stations",
"main": {
"temp":294.787,
"pressure":1024.49,
"humidity":52,
"temp_min":294.787,
"temp_max":294.787,
"sea_level":1032.69,
"grnd_level":1024.49
}, ...
Let's use curl!
curl -v "http://api.openweathermap.org/data/2.5/weather?\
q=Berkeley,ca"
HTTP/1.1 401 Unauthorized
Server: openresty
Date: Fri, 02 Nov 2018 12:34:35 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 107
Connection: keep-alive
X-Cache-Key: /data/2.5/weather?q=berkeley,ca
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST
Connection #0 to host api.openweathermap.org left intact
{"cod":401, "message": "Invalid API key.
Please see http://openweathermap.org/faq#error401 for more info."}
From website: Starting from 9 October 2015 our API requires a valid APPID for access. Note that this does not mean that our API is subscription-only now - please take a minute to register a free account to receive a key.
curl -v "http://api.openweathermap.org/data/2.5/weather?\
q=Berkeley,ca&appid=af578739923ac7f173a6054b24c606ea"
Note the Content Type
HTTP/1.1 200 OK
Server: openresty
Date: Thu, 20 Oct 2016 22:54:46 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 461
{
"coord": {
"weather": [
{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01d"
...