REST DAN JSON
Husni Husni.trunojoyo.ac.id
REST DAN JSON Husni Husni.trunojoyo.ac.id Web 2.0 What is Web - - PowerPoint PPT Presentation
REST DAN JSON Husni Husni.trunojoyo.ac.id Web 2.0 What is Web 2.0? Commonly associated with web applications that facilitate interactive information sharing, interoperability, user-centered design and collaboration on the WWW.
Husni Husni.trunojoyo.ac.id
interactive information sharing, interoperability, user-centered design and collaboration on the WWW.
but also programmable web.
browser
Communication:
source; allow extensions by users.
posts, notes, photo albums, etc.) shared each week
1 Taken from http://www.facebook.com/press/info.php?statistics on June 6th, 2010.
1 Taken from http://en.wikipedia.org/wiki/Special:Statistics on June 6th, 2010.
1 Taken from http://www.viralblog.com/research/youtube-statistics on June 6th, 2010.
advertisements – with sections devoted to jobs, housing, personals, for sale, services, community, gigs, résumés, and discussion forums.
from the fact that it is "in no way affiliated with craigslist or Google”
APIs
computational platform
such as the World Wide Web.
"representations" of "resources".
implementation
communicate via a standardized interface (e.g., HTTP)
conveying the information).
tunnels, etc.) can mediate the request, but each does so without being concern about anything but its own request
identifier of the resource and the action required
firewalls, tunnels, or anything else between it and resource
(representation) returned.
each server
as long as the interface is not altered.
to service the request.
server interactions, further improving scalability and performance.
to further requests.
a client by transferring to it logic that it can execute.
client-side scripts such as JavaScript.
Hotel booking service service description search results hotel info confirmation my bookings payment
Example: hotel booking
Hotel booking workflow
1.
Retrieve service description
2.
Submit search criteria according to description
3.
Retrieve linked details of interesting hotels
4.
Submit payment details according to selected rate description
5.
Retrieve confirmation of booking
Example: hotel booking
Hotel booking service service description search results hotel info confirmation my bookings payment Hotel booking service service description search results hotel info confirmation my bookings payment
search(date, city)
list of hotels & rates
getHotelDetails(hotel)
hotel details
reserve(rate, creditCard)
confirmationID
getConfirmationDetails(confID)
confirmation details
listMyBookings()
list of confirmationIDs
hypermedia -> operations
Example: hotel booking
the identified resource:
The data is included in the body of the request.
developer's toolkit.
Types, MIME Types)
enable its customers to:
HTTP POST URL 3
PO (HTML/XML)
HTTP GET request URL 1 HTTP response URL to submitted PO Parts List Part Data PO HTTP response
Response (HTML/XML doc)
HTTP response
Response (HTML/XML doc)
HTTP GET request URL 2
<?xml version="1.0"?> <Parts> <Part id="00345" href="http://www.parts-depot.com/parts/00345"/> <Part id="00346" href="http://www.parts-depot.com/parts/00346"/> <Part id="00347" href="http://www.parts-depot.com/parts/00347"/> <Part id="00348" href="http://www.parts-depot.com/parts/00348"/> </Parts>
Note that the parts list has links to get detailed info about each part. This is a key feature of the REST design pattern. The client transfers from one state to the next by examining and choosing from among the alternative URLs in the response document.
<?xml version="1.0"?> <Part> <Part-ID>00345</Part-ID> <Name>Widget-A</Name> <Description>This part is used within the frap assembly</Description> <Specification href="http://www.parts-depot.com/parts/00345/specification"/> <UnitCost currency="USD">0.10</UnitCost> <Quantity>10</Quantity> </Part>
Again observe how this data is linked to still more data - the specification for this part may be found by traversing the hyperlink. Each response document allows the client to drill down to get more detailed information.
What if Parts Depot has a million parts, will there be a million static pages? For example: http://www.parts-depot/parts/000000 http://www.parts-depot/parts/000001 ... http://www.parts-depot/parts/999999
We need to distinguish between a logical and a physical entity. The above URLs are logical. They express what resource is desired. They do not identify a physical object. The advantage of using a logical identifier (URL) is that changes to the underlying implementation of the resource will be transparent to clients (that's loose coupling!). Quite likely, Parts Depot will store all parts data in a database. Code at the Parts Depot web site will receive each logical URL request, parse it to determine which part is being requested, query the database, and generate the part response document to return to the client.
Contrast the above logical URLs with these physical URLs: http://www.parts-depot/parts/000000.html http://www.parts-depot/parts/000001.html ... http://www.parts-depot/parts/999999.html These URLs are clearly pointing to physical (HTML) pages. If there are a million parts it will not be very attractive to have a million static pages. Furthermore, changes to how these parts data is represented will effect all clients that were using the old representation.
For complex queries, Parts Depot will provide a service (resource) for a client to retrieve a form that the client then fills in. When the client hits "Submit" the browser will gather up the client’s responses and generate a URL based on the responses. Thus, oftentimes the client doesn't generate the URL (think about using Amazon - you start by entering the URL to amazon.com; from then on you simply fill in forms, and the URLs are automatically created for you).
That is, the information that a resource returns to a client should link to other information in a network of related information.
done with simple operations. Most web interactions are done using HTTP and just four operations:
Data transfer encoding
“JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate” – JSON.org JSON is a subset of JavaScript
Name/Value Pairs Number data type String Array Child properties
number string value
false null array true
string " Any UNICODE character except " or \ or control character \ " \ quotation mark reverse solidus / solidus b backspace formfeed newline carriage return horizontal tab 4 hexadecimal digits f n r t u "
number digit 1 - 9 . digit e E digit
+
{ : } value string
,
{"name":"Jack B. Nimble","at large": true,"grade":"A","level":3, "format":{"type":"rect","width":1920, "height":1080,"interlace":false, "framerate":24}}
{ "name": "Jack B. Nimble", "at large": true, "grade": "A", "format": { "type": "rect", "width": 1920, "height": 1080, "interlace": false, "framerate": 24 } }
[ ] value array ,
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] [
[0, -1, 0], [1, 0, 0], [0, 0, 1]
]
responseData = JSON.parse(responseText); jsonstring = '{ "name": "Jack B. Nimble", "at large": true, "grade": "A", "format": { "type": "rect", "width": 1920, "height": 1080, "interlace": false, "framerate": 24 } }'; //jsonobject = eval('(' + jsonstring + ')'); jsonobject = JSON.parse(jsonstring); document.write("The object<br>"); document.write("name: ", jsonobject.name, "<br>"); document.write("grade: ", jsonobject.grade, "<br>"); document.write("format: ", jsonobject.format, "<br>");
the page. eval of the data is no less secure than the
not protect our interests.
any human language to be communicated;
names as well as specific values;
necessary parsing algorithms to remain simple, efficient, and consistent;
data structures: records, lists and trees.
JSON XML Data Structure Data Structure No validation system XSD No namespaces Has namespaces (can use multiples) Parsing is just an eval
Parsing requires XML document parsing using things like XPath In JavaScript you can work with
types In JavaScript you can work with strings – may require additional parsing Security: Eval() means that if the source is not trusted anything could be put into it. Libraries exist to make parsing safe(r) Security: XML is text/parsing – not code execution.
and you want to expose a public API to build apps.
Issue JSON XML The public will be parsing data
make it secure. Run checks against the data in the object to make sure it’s secure. You are working
check for potential code access issues. Run checks against the data to make sure it’s secure. Data must be in a specific format. Build something that parses the objects. XML Schema
your front end needs to show entries from a mailbox, but needs to be dynamic and so you will use a lot of JavaScript.
Issue JSON XML Your in house developers know objects and would like to use them. JSON is JavaScript
Write JavaScript to parse the XML to objects. The site is secure but you worry about people checking the page source. You page has JavaScript in it and (maybe) code which communicates with a private backend server. No major issues. You page has JavaScript in it and (maybe) code which communicates with a private backend
weaknesses and you need to identify when one is stronger than the other.