API
API (application programming interface) 是一种软件接口,为调用方提供服务和功能,而调用方无需知道实现细节。
这是一个抽象概念,如果举例说明,那就是在餐厅拿菜单点餐,菜单就是餐厅给客户的 API,我们点餐就是在调用 API,而具体怎么做这个食物我们不关心,我们只要能得到食物就行。
Web API
注意 MDN 文档所提的 Web API 是指 client-side Web API,即 Browser/JavaScript libraries/JavaScript framework API。
互联网开发者一般说的 API 都是 Web API,是一种基于 HTTP 的请求响应消息架构(Request–response message architecture)。
例如下面请求:
GET https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=1996-12-03
其中 api_key
和 date
都是参数名,DEMO_KEY
和 1996-12-03
都是参数值。
响应结果:
{
"date":"1996-12-03",
"explanation":"Like a butterfly,\r a white dwarf star begins its life\r by casting off a cocoon that enclosed its former self. In this\r analogy, however, the Sun would be\r a caterpillar\r and the ejected shell of gas would become the prettiest of all!\r The above cocoon, the planetary nebula\r designated NGC 2440, contains one of the hottest white dwarf stars known.\r The white dwarf can be seen as the bright dot near the photo's\r center. Our Sun will eventually become a \"white dwarf butterfly\",\r but not for another 5 billion years. The above false color image recently entered the public domain\r and was post-processed by F. Hamilton.\r",
"hdurl":"https://apod.nasa.gov/apod/image/9612/ngc2440_hst2_big.jpg",
"media_type":"image",
"service_version":"v1",
"title":"Cocoon of a New White Dwarf\r\nCredit:",
"url":"https://apod.nasa.gov/apod/image/9612/ngc2440_hst2.jpg"
}