270 likes | 389 Views
WEB API. API. is a set of subroutine definitions, protocols, and tools for building application software. Purpose API make it easier for developers to use certain technologies in building applications. API. APPLICATION PROGRAMMING INTERFACE. API. Documentation describing API:
E N D
is a set of subroutine definitions, protocols, and tools for building application software. Purpose API make it easier for developers to use certain technologies in building applications. API APPLICATION PROGRAMMING INTERFACE
API • Documentation describing API: • List of functions (endpoints) • Calls (requests) • Returns (responses) • Implementations: • Library or framework that can be imported to develop an application • OS interfaces between an app and OS (POSIX, MacOS API, Windows API) • Remote APIs on remote servers
WEB (Remote) APIs • A server-side programmatic interface allows the client application to communicate with the server • Based on one or more publicly exposed endpointsand a defined request–response message system. • Uses the network to provide its services
NET Protocols • Address resolving and delivering packets • Transmission Control between applications over IP • Content container Level (application protocol) • Security Level • IP • TCP • HTTP • HTTPS • URL: Protocol + address + port + path on server (URI) _https://best.server.com:8080/API/endpoints/cars?brand=FERRARI&color=RED
Web Services zhttps://best.server.com:8080/API/endpoints/cars?brand=FERRARI&color=RED DesktopApps INTERNET Endpoint Mobile apps WEB SERVER Requests Endpoint DB Endpoint Other servers Responses Endpoint Web API Endpoint Smart Teapot
is an architectural style URL based services HTTP/HTTPS only JSON , XML, TEXT, HTML Authorization and security must be implemented separately Easy to implement SOAP vs REST SimpleObjectAccess Protocol REpresentationalStateTransfer • is a protocol • uses services interfaces • Works on top of any communication Protocol, even asynchronously (TCP HTTP FTP SMTP) • Only XML • Security and authorization are part of the protocol • Fully described and provide information to users (WSDL)
Principles of REST • RESOURCES expose easily understood directory structure URIs. • REPRESENTATIONS transfer JSON or XML to represent data objects and attributes. • MESSAGES use HTTP methods explicitly (for example, GET, POST, PUT, and DELETE). • STATELESS interactions store no client context on the server between requests. The client holds session state.
HTTP methods • GET - Retrieve information. GET requests must be safe and idempotent, meaning regardless of how many times it repeats with the same parameters, the results are the same • POST - Request that the resource at the URI do something with the provided entity • PUT - Store an entity at a URI. PUT can create a new entity or update an existing one. • PATCH - Update only the specified fields of an entity at a URI • DELETE - Request that a resource be removed; however, the resource does not have to be removed immediately. • HEAD, OPTIONS, TRACE and CONNECT
SELECT rows INSERT new row UPDATE row DELETE row UPDATE certain field CRUD - HTTP REQUESTS IN REST HTTP Requests DB operations • GET • POST • PUT • DELETE • PATCH
HTTP request status codes indicate the result of the HTTP request. • 1XX - informational • 2XX – success (200 -OK, 201 – Created,204 - No Content) • 3XX - redirection • 4XX - client error (400-Bad Request, 401 Unauthorized, 403 - Forbidden, 404 - PNF) • 5XX - server error
Request Structure • A request line GET /images/logo.png HTTP/1.1 • Request header fields Accept-Language: en Content-Type: text/html Cookie: <cookie-list> • An empty line. • An optional message body. JSON, TEXT, XML
Response Structure • A status line which includes the status code and reason message HTTP/1.1 200 OK • Response header fields Content-Type: text/html Allow: GET, HEAD. • An empty line. • An optional message body JSON, TEXT, XML