Introduction to Web Services
Asst. Prof. Chaiporn Jaikaeo, Ph.D.
[email protected] http://www.cpe.ku.ac.th/~cpj
Computer Engineering Department
Kasetsart University, Bangkok, Thailand
Traditional World-Wide-Web
Designed for human-to-app interactions
Information sharing
(Non-automated) E-commerce
Built on top of
HTTP – for data transfer
HTML – for representing document
2
What's Next?
Try taking humans out of the loop
Systematic application-to-application
interaction over the web
Automated e-commerce
Resource sharing
Distributed computing
Web services
(another) effort to build distributed computing
platform for the web
3
Web Services vs. Web Apps
Web application
Provides service directly to user
browser web server
HTTP
Web service or (Web API)
Provides service to other programs
web server
(service provider)
app HTTP
(service user)
4
Web Service Protocols
Simple Object Access Protocol (SOAP)
Service requests and responses are always done
via XML messages, called SOAP envelope
Formally supported by World Wide Web
Consortium (W3C)
Representational State Transfer (REST)
Service requests are done via generic HTTP
commands: GET, POST, PUT, DELETE
Simpler set of operations
5
Other Web Service Protocols
Web feeds
RSS
Atom
Remote procedure call
JSON-RPC
XML-RPC
JavaScript
6
Sample SOAP Message
E.g., message requesting IBM stock price
POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 299 HTTP Header
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
</soap:Header>
<soap:Body>
<m:GetStockPrice xmlns:m="http://www.example.org/stock">
<m:StockName>IBM</m:StockName>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope> SOAP Envelope
Desired operation Object of interest
(Verb) (Noun)
7
Sample REST Request
E.g., message requesting IBM stock price
GET /stock/ibm HTTP/1.1
Host: www.example.org HTTP Header
Desired operation Object of interest
(Verb) (Noun)
The above corresponds to the URL
http://www.example.org/stock/ibm
8
Data Interchange Formats
XML – eXtensible Markup Language
E.g., RSS, Atom
<?xml version="1.0" encoding="ISO-8859-1"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/">
<title>Slashdot</title>
<link>http://slashdot.org/</link>
<description>News for nerds, stuff that matters</description>
<dc:subject>Technology</dc:subject>
<items>
<rdf:Seq>
<rdf:li rdf:resource="http://it.slashdot.org/story/11/02/14/0337211/Recent-HP-
Laptops-Shipped-CPU-Choking-Wi-Fi-Driver?from=rss" />
</ref:Seq>
</items>
</rdf:RDF>
Large collection of libraries for processing XML
9
Data Interchange Formats
JSON – JavaScript Object Notation
Can be directly evaluated in JavaScript
Typically smaller than XML
Libraries for other languages are also available
{ <Person>
"Person": <firstName>John</firstName>
{ <lastName>Smith</lastName>
"firstName": "John", <age>25</age>
"lastName": "Smith", <Address>
"age": 25, <streetAddress>21 2nd Street</streetAddress>
"Address": <city>New York</city>
{ <state>NY</state>
"streetAddress":"21 2nd Street", <postalCode>10021</postalCode>
"city":"New York", </Address>
"state":"NY", <PhoneNumbers>
"postalCode":"10021" <home>212 555-1234</home>
}, <fax>646 555-4567</fax>
"PhoneNumbers":
{
</PhoneNumbers>
</Person> XML
"home":"212 555-1234",
"fax":"646 555-4567"
}
}
}
JSON
10
Example –Wikipedia
RESTful
API reference
http://en.wikipedia.org/w/api.php
Example
http://en.wikipedia.org/w/api.php?format=json&action=q
uery&titles=Kasetsart%20University&prop=revisions&rvp
rop=content
11
Example – HostIP.info
RESTful
Determines geographical location of the
specified IP address
API documentation
http://www.hostip.info/use.html
Example
http://api.hostip.info/get_json.php?ip=158.108.2.71
12
Coding Example
E.g., accessing HostIP.info from Python
import sys
import json
from urllib import urlencode
from urllib2 import urlopen
response = urlopen('http://api.hostip.info/get_json.php?' +
urlencode({
'ip' : sys.argv[1],
}))
data = json.loads(response.read())
print 'IP Address: %s' % data['ip']
print 'City: %s' % data['city']
print 'Country: %s' % data['country_name']
13
Mashups
A mashup is a webpage or application that offers
new services by combining data or functionality
from two or more sources
Main characteristics of mashups
Combination
Visualization
Aggregation
They aim to make existing data more useful
Example: Wikipediavision
Google Maps + Wikipedia
http://www.lkozma.net/wpv/index.html
14
More Resources
Search engine for web APIs and mashups
http://www.programmableweb.com
15