308364 restful web service and json

24
ICT@PSU 308-364 Advanced Web Programming 1 of 24 RESTful Web Service and JSON 308-364 Advanced Web Programming 1/2558 Simplicity is the ultimate sophistication Leonardo da Vinci Web Service Client Web Service Server HTTP Request Method (GET, POST, PUT, DELETE, ) Data (XML, JSON) Internet

Upload: worapot-jakkhupan

Post on 22-Jan-2017

712 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 1 of 24

RESTful Web Service and JSON308-364 Advanced Web Programming

1/2558

Simplicity is the ultimate sophistication

Leonardo da Vinci

Web Service

Client

Web Service

Server

HTTP Request Method

(GET, POST, PUT, DELETE, …)

Data (XML, JSON)

Internet

Page 2: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 2 of 24

Web Services• A Web service is a method of communication between two electronic

devices over a network. It is a software function provided at a network address over the Web with the service always on as in the concept of utility computing. (Wiki)

• The W3C defines a Web service generally as: a software system designed to support interoperable machine-to-machine interaction over a network.

• A web service a software that makes itself available over the internet. The web services are not tied to any one operating system or programming language.• Java can talk with Perl • Windows applications can talk with Unix applications

• Web services are open standard (XML, SOAP, HTTP etc.) based Web applications that interact with other web applications for the purpose of exchanging data.

Page 3: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 3 of 24

Web Services• To summarize, a complete web service is, therefore, any service

that:• Is available over the Internet or private (intranet) networks

• Uses a standardized (XML, JSON) messaging system

• Is not tied to any one operating system or programming language

• Is discoverable via a simple find mechanism

Page 4: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 4 of 24

Web Service Architecture

• Service Provider• The service provider implements the service and makes it available on the Internet.

• Service Consumer• The consumer utilizes an existing web service by opening a network connection and

sending a request.

• Service Directory• This is a logically centralized directory of services which provides a central place where

provider can publish new services, and the consumer can find the existing services.

Service Directory

Service Consumer Service ProviderBinds

Find Publish

Page 5: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 5 of 24

Protocol

• Web services are of two kinds: Simple Object Access Protocol (SOAP) and Representational State Transfer (REST).

• SOAP • defines a standard communication protocol (set of rules) specification for

XML-based message exchange. • SOAP uses different transport protocols, such as HTTP and SMTP.

• REST • describes a set of architectural principles by which data can be transmitted

over a standardized interface (such as HTTP). • REST does not contain an additional messaging layer and focuses on

design rules for creating stateless services. • A client can access the resource using the unique URI and a representation

of the resource is returned.

Page 6: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 6 of 24

SOAP• The Web Services Description Language (WSDL) contains and describes

the common set of rules to define the messages, bindings, operations and location of the Web service. WSDL is a sort of formal contract to define the interface that the Web service offers.

• SOAP requires less plumbing code than REST services design. Most real-world applications are not simple and support complex operations, which require conversational state and contextual information to be maintained.

• With the SOAP approach, developers need not worry about writing this plumbing code into the application layer themselves.

• SOAP supports several protocols and technologies, including WSDL, XSDs, SOAP, WS-Addressing

Page 7: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 7 of 24

Representational State Transfer (REST)

• Introduced by Roy Fielding in his doctoral dissertation

• The RESTful Web services are completely stateless. REST-based implementation is simple compared to SOAP.

• Restful services provide a good caching infrastructure over HTTP GET method (for most servers).

• There is no standard set of rules to describe the REST Web services interface.

• This makes developers more productive and comfortable as they will not have to rewrite everything from scratch and just need to add on the existing functionality.

Page 8: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 8 of 24

SO

AP

vs

RE

ST

http://www.computerworld.com/article/2506308/app-development/image-gallery--grab-some-rest-with-your-soap.html

Page 9: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 9 of 24

http://www.pwc.com/us/en/technology-forecast/2012/issue2/features/feature-consumerization-apis.html

Page 10: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 10 of 24

RESTful• REST stands for REpresentational State Transfer. REST is web

standards based architecture and uses HTTP Protocol for data communication.

• It revolves around resource where every component is a resource and a resource is accessed by a common interface using HTTP standard methods.

• REST uses various representations to represent a resource like text, JSON and XML.

• Now a days JSON is the most popular format being used in web services.

Page 11: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 11 of 24

HTTP Methods

• Following well known HTTP methods are commonly used in REST based architecture.• GET - Provides a read only access to a resource.

• PUT - Used to create a new resource.

• DELETE - Used to remove a resource.

• POST - Used to update a existing resource or create a new resource.

• OPTIONS - Used to get the supported operations on a resource.

Page 12: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 12 of 24

API Data Exchange• API designers these days tend to land on one of two formats for

exchanging data between their servers and client developers -XML or JSON.

• Though a number of different formats for data have been designed and promoted over the years, XML's built in validation properties and JSON's agility have helped both formats emerge as leaders in the API space.

Page 13: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 13 of 24

XML - Ideal for Highly Structured Information

• eXtensible Markup Language (XML)• A set of rules for encoding documents electronically.

• De-facto standard (W3C Recommendation).

• As such it can not be avoided as a possible data format for Web 2.0 Web Services.

• As opposed to JSON, XML can be verified against a schema expressed in a number of languages such as Document Type Definition (DTD), and XML Schema:• the vocabulary (element and attribute names),

• the content model (relationships and structure), and

• the data types.

Page 14: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 14 of 24

JSON• JavaScript Object Notation (JSON)• A lightweight computer data interchange format.

• Specified in Request For Comment (RFC) 4627.

• Represents a simple alternative to XML. A text-based, human-readable format for representing simple data structures and associative arrays (called objects).

• Used by a growing number of services

• JavaScript-friendly notation• Its main application is in Ajax Web application programming.

• A serialized object or array. No namespaces, attributes. No schema language (for description, verification)

Page 15: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 15 of 24

XML vs JSONJSON• Pros:• Simple syntax, which results in

less "markup" overhead compared to XML.

• Easy to use with JavaScript as the markup is a subset of JS object literal notation and has the same basic data types as JavaScript.

• JSON Schema for description and datatype and structure validation

• Cons:• Simple syntax, only a handful of

different data types are supported.

XML• Pros:• Generalized markup; it is possible

to create "dialects" for any kind of purpose

• XML Schema for datatype, structure validation. Makes it also possible to create new datatypes

• XSLT for transformation into different output formats

• Built in support for namespaces

• Cons:• Relatively wordy compared to

JSON (results in more data for the same amount of information).

Page 16: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 16 of 24

Example of XML and JSON

Page 17: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 17 of 24

JSON Object and Array• JSON Object• Squiggly brackets act as 'containers' • {...}• Names and values are separated by a colon • { Name : Value }

• JSON Array• Square brackets holds arrays • [...]• Array elements are separated by commas • [member1, member2]

Page 18: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 18 of 24

1) JSON Object

• An object is an unordered set of name/value pairs

• An object begins with { (left brace) and ends with } (right brace)

• Each name is followed by :(colon) and the name/ value pairs are separated by ,(comma)

<numbers>

<4>four</4>

<8>eight</8>

</numbers>

{

numbers: {

4: “four”,

8: “eight”

}

}

Page 19: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 19 of 24

2) JSON Array

• An array is an ordered collection of values

• An array begins with [ (left bracket) and ends with ] (right bracket)

• Values are separated by ,(comma).

{

fruits: [

“Apple”,

“Banana”,

“Pear”

]

}

<fruits>

<0>Apple</0>

<1>Banana</1>

<2>Pear</2>

</fruits>

Page 20: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 20 of 24

{

"section": {

"title": "Book-Signing Event",

"signing": [

{

"author": {

"title": "Mr",

"name": "Vikram Seth"

},

"book": {

"title": "A Suitable Boy",

"price": "$22.95"

}

},

{

"author": {

"title": "Dr",

"name": "Oliver Sacks"

},

"book": {

"title": "The Island of the Color-Blind",

"price": "$12.95"

}

}

]

}

}

• What is the name of this

event?

• How many books are

there?

• Names and prices of the

books.

• Who is the author of each

book?

section.titlesection.signing[0].author.titlesection.signing[1].book.title

Page 21: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 21 of 24

JSON Viewer

Page 22: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 22 of 24

Implement a REST service provider

http://localhost/rest_json.php?callback=?&name=TEST&email=TEST

2) Get the data from service

consumer using HTTP GET

3) Create a JSON object

using name-value pairs

4) Call a json_encode

function to transform the

array into JSON data format

1) Define the header

5) Require callback to ignore

data catch

Page 23: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 23 of 24

Call a web service using $.getJSON()

1) Import jQuery

2) Define end-point URI

3) Passing variables (name, email)

4) Callback function

gets the returned data5) Extract value

from returned

data

Page 24: 308364 RESTful Web Service and JSON

ICT@PSU 308-364 Advanced Web Programming 24 of 24

Assignment 5 (10%)• You will be given a username/password to access a MySQL server.• Following this guideline, create a RESTful web service provider using

PHP (4%)• http://www.webbookthai.com/?p=615• Describe the data in the given database.• You need to construct the JSON structure by yourself, and you must

describe it clearly.• Following this guideline, implement a web service consumer using

$.getJSON() to call the web service that you've created (4%)• http://www.webbookthai.com/?p=622• You may use front-end web development framework such as Bootstrap

to decorate your website. • Don't forget to record and submit an assignment report (2%), the more

you record, the high score you will get.