LOGO
UNIV/POLTEK
DTS 2019
Internet of Things
22 REST API
digitalent.kominfo.go.id
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Topik Bahasan
• Overview
• API
• REST
• JSON
• XML
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Praktek
• Mengakses REST API Github
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
API
• Application Programming Interface is a set of routines,
protocols, and tools for building software applications.
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Beberapa Jenis API
• Representational State Transfer (REST)
• Remote Procedure Calls (RPC)
• Simple Object Access Protocol (SOAP)
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
REST
• REST kependekan dari REpresentational State Transfer
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
REST
• The REST architectural style describes six constraints.
• The six constraints are:
• Uniform Interface
• Stateless
• Cacheable
• Client-Server Separation
• Layered System
• Code on Demand (optional)
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
REST API
• Generality
• Familiarity
• Scalability
• Segmentation
• Speed
• Security
• Encapsulation
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
REST API
• Berjalan di atas protokol HTTP
• Menggunakan JSON atau XML untuk mengirimkan data
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Anatomy of REST API Request
• endpoint
• method
• headers
• body
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Endpoint
• Endpoint consists of root-endpoint and path.
• The root-endpoint is the starting point of the API you’re
requesting from.
• For example, the root-endpoint of Github’s API is
https://api.github.com while the root-endpoint Twitter’s
API is https://api.twitter.com.
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Endpoin
• The path determines the resource you’re requesting for.
• Think of it like an automatic answering machine that
asks you to press 1 for a service, press 2 for another
service, 3 for yet another service and so on.
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Endpoint
• You can access paths just like you can link to parts of a
website.
• For example, to get a list of all posts tagged under
“JavaScript” on Smashing Magazine, you navigate to
http://cybertrust.biz.id/tag/javascript/.
• http://cybertrust.biz.id / is the root-endpoint and
/tag/javascript is the path.
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Method
The method is the type of request you send to the server.
You can choose from these 4 types below:
• GET
• POST
• PUT
• DELETE
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Method
Method Request Meaning
This request is used to get a resource from a server. If you perform a GET request, the server looks
GET for the data you requested and sends it back to you. In other words, a GET request performs a
READ operation. This is the default request method.
This request is used to create a new resource on a server. If you perform a POST request, the
POST server creates a new entry in the database and tells you whether the creation is successful. In other
words, a POST request performs an CREATE operation.
These two requests are used to update a resource on a server. If you perform a PUT request, the
PUT server updates an entry in the database and tells you whether the update is successful. In other
words, a PUT request performs an UPDATE operation.
This request is used to delete a resource from a server. If you perform a DELETE request, the
DELETE server deletes an entry in the database and tells you whether the deletion is successful. In other
words, a DELETE request performs a DELETE operation.
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Header
• Headers are used to provide information to both the
client and server.
• It can be used for many purposes, such as authentication
and providing information about the body content.
• Header example: "Content-Type: application/json"
• You can find a list of valid headers on MDN’s HTTP
Headers Reference.
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Body
• The body contains information you want to be sent to
the server.
• Body example: property1=value1
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Another Example
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
JSON (JavaScript Object Notation)
• Better Language Support
• Lightweight (much less code than XML)
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
JSON (JavaScript Object Notation)
{ "name":"John",
"age":30,
"car":null
}
{ 'sensorId': 'temp1',
'Value': 25
}
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
XML (eXtensible Markup Language)
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Authentication
There are two main ways to authenticate yourself:
• With a username and password (also called basic
authentication)
• With a secret token
The secret token method includes oAuth, which lets you to
authenticate yourself with social media networks like
Github, Google, Twitter, Facebook, etc.
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Secret Token
• Users should be provided with a unique API key/
identifier that allows you to track, limit, and enable
features
• An OAuth token should be used to link API keys to
accounts instead of requesting account usernames and
passwords
• Tokens help prevent misuse of the system and limit acce
ss to the control panel.
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Praktek
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Mengakses REST API Github
• You can send a request with any programming
language.
• JavaScript users can use methods like the Fetch
API and jQuery’s Ajax method.
• Ruby users can use Ruby’s Net::HTTP class.
• Python users can use Python Requests.
• Here we’ll use the command line utility called cURL.
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Mengakses REST API Github
• curl https://api.github.com
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Mengakses REST API Github
• curl https://api.github.com/users/zellwk/repos
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Mengakses REST API Github
• curl
https://api.github.com/users/zellwk/repos\?sort\=pushe
d
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Mengakses REST API Github
• curl -X POST https://api.github.com/user/repos
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Mengakses REST API Github
• curl -H "Content-Type: application/json"
https://api.github.com
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Mengakses REST API Github
• curl -H "Content-Type: application/json"
https://api.github.com -v
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Mengakses REST API Github
curl -X POST https://requestb.in/1ix963n1 \
-d property1=value1 \
-d property2=value2
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Mengakses REST API Github
curl -X POST https://requestb.in/1ix963n1 \
-H "Content-Type: application/json" \
-d '{
"property1":"value1",
"property2":"value2"
}'
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
Mengakses REST API Github
curl -x POST -u "username:password"
https://api.github.com/user/repos
digitalent.kominfo.go.id
LOGO
UNIV/POLTEK
IKUTI KAMI
digitalent.kominfo
digitalent.kominfo
DTS_kominfo
Digital Talent Scholarship 2019
Pusat Pengembangan Profesi dan Sertifikasi
Badan Penelitian dan Pengembangan SDM
Kementerian Komunikasi dan Informatika
Jl. Medan Merdeka Barat No. 9
(Gd. Belakang Lt. 4 - 5)
Jakarta Pusat, 10110
digitalent.kominfo.go.id
digitalent.kominfo.go.id