
Fast Guide to RESTful APIs and HTTP Commands
This content covers the basics of RESTful APIs, HTTP commands, and the key principles of REST architecture such as uniform interface, client-server decoupling, statelessness, cacheability, layered system architecture, and code on demand. It compares REST with SOAP briefly and provides insights into using different HTTP methods for requests and responses, along with practical examples. The guide aims to help readers understand how to interact efficiently with APIs and leverage HTTP commands effectively.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
FLUTTER and API REST esercitazione elabFTW
COSE UN APPLICATION PROGRAMMING INTERFACE COS E UN APPLICATION PROGRAMMING INTERFACE openAPI 2
REST vs SOAP in breve REST vs SOAP in breve Simple Object Access Protocol Simple Object Access Protocol (SOAP)! (SOAP)! POST POST GET, xml is an envelope GET, xml is an envelope HTTP POST /ReturnValue HTTP/1.0 Host: www.abc.net Content-Type: text/xml; charset = utf-8 Content-Length: nnn <?xml version = "1.0"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope" SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding"> <SOAP-ENV:Body xmlns:m = "http://www.abc.net/values"> <m:Value> <m:ValueName>Temperature</m:ValueName> </m:GetValue> </SOAP-ENV:Body> </SOAP-ENV:Envelope> HTTP/1.0 200 OK Content-Type: text/xml; charset = utf-8 Content-Length: nnn <?xml version = "1.0"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope" SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding"> <SOAP-ENV:Body xmlns:m = "http://www.abc.net/values"> <m: ValueResponse > <m: Value >25.0</m: Value> </m: ValueResponse > </SOAP-ENV:Body> </SOAP-ENV:Envelope> How to move fast : HTTP GET http://www.abc.net?value=temperature -> Response With RESTful: HTTP GET http://www.abc.net/values/temperature -> 25.0 (in JSON: { temperature : 25.0}) also: let s use all the commands available in HTTP 3
HTTP Commands HTTP Commands Request has payload body Optional Optional Yes Yes Optional Optional Optional No Yes Response has payload body Yes No Yes Yes Yes Yes Yes Yes Yes Request method RFC GET HEAD POST PUT DELETE CONNECT OPTIONS TRACE PATCH RFC 9110 RFC 9110 RFC 9110 RFC 9110 RFC 9110 RFC 9110 RFC 9110 RFC 9110 RFC 5789 https://en.wikipedia.org/wiki/HTTP 4
RESTful RESTful Uniform interface Uniform interface: One piece of data belong to a single URI Client Client- -server decoupling server decoupling: You only need to know the URI of the server to interact Statelessness Statelessness: all the information to process is included in the message, no previous operations required Cacheability Cacheability: everything that is cacheable must be cached, to improve performance (client AND server side) Layered system architecture Layered system architecture: the architecture is transparent to the layers inside Code on demand* Code on demand*: could give runnable code as a response (this is risky and must be done only on-demand). 5
elabFTW elabFTW https://www.elabftw.net/ 6
generate a personal API Key generate a personal API Key https://www.elabftw.net/ 7
Lets see if the key works Let s see if the key works Unix: curl https://prp-electronic-lab.areasciencepark.it/api/v2/experiments -H "Authorization: [INSERT YOUR KEY HERE] - GET the list of experiments Windows POWERSHELL: Invoke-WebRequest -Uri "https://prp-electronic-lab.areasciencepark.it/api/v2/experiments/56" -Method Get - Headers @{ Authorization = '[INSERT YOUR KEY HERE] } POST/PATCH: Unix: curl -i -H "Content-Type: application/json" -H "Authorization: [INSERT YOUR KEY HERE] " -X PATCH -d '{"title": "test patch", "date": "2024-04-29", "body": "elabFTW API patch"} https://prp-electronic- lab.areasciencepark.it/api/v2/experiments/[Experiment id] Windows POWERSHELL: Invoke-WebRequest -Uri "https://prp-electronic-lab.areasciencepark.it/api/v2/experiments/[Experiment id]" -Method POST -Headers @{ Authorization = '[INSERT YOUR KEY HERE] } -Body @{title = test } 8
WHAT WE NEED WHAT WE NEED Packages: import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; add http: ^1.2.0 under dependencies in pubspec.yaml If you are debugging with browsers/macos, add web-browser-flag disable-web-security 9