T



Web Services GOALS:Advanced and intensive training Workshop on Web Services.Address full stack web programming: Client+Server+DatabaseAt the end of the workshop you'll be able to create a real web service at your CS account and know how to build clients to use a web service. TABLE OF CONTENTS:What is a Web Service ?The CS Linux system (same as in the Web Workshop)SET-UP the web space for WEB SERVICES on the CS Linux serverExample A (a simple Web Service without Database which returns RAW data)Example B (a simple Web Service without Database which returns multiple items in JSON)Example C (a simple Web Service with Database which returns multiple items in JSON)Example D (a simple Web Service with Database which returns multiple items in JSON)DEBUGING your programsReferences 1. What is a Web Service ?Web services?or Web?APIs are remote procedures/functions/methods which can be called/used over the WWW. Each of these methods usually provides a service or implements a task, it can have parameters and returns a result (just like a Java method). A Web service/API can be called/requested in/from a client which is a Web browser, a program on a PC, or a mobile app. Web services are executed on a remote/web server system to provide services (access database or files, runs another program, …). The CLIENT asks a “question” (make a request, call with parameters) and the SERVER responses with an “answer” (results).Other definitions:A?Web service?is a software?service?used to communicate between two devices on a network. More specifically, a?Web service?is a software application with a standardized way of providing interoperability between disparate applications. It does so over HTTP using technologies such as XML, SOAP, WSDL, and UDDI.As an?example, Amazon provides a?web service?that provides prices for products sold online via . ...?Web services?use a standard such as SOAP (Simple Object Access Protocol) for sending the data (which can be in XML, JASON, …) between applications. The data is sent over normal HTTP.Types of Web ServicesThere are mainly two types of web services:SOAP Web ServicesRESTful Web ServicesSOAP (Simple Object Access Protocol):SOAP stands for Simple Object Access Protocol. It is an XML- based protocol for accessing web services. SOAP is a W3C recommendation for communication between two applications. It is platform-independent and language-independent. Using SOAP, you will be able to interact with other programming language applications.Advantages of SOAP Web Services:WS Security:?SOAP defines its own security known as WS Security.Language and Platform?Independent: SOAP web services can be written in any programming language and executed in any platform.Disadvantages of SOAP Web Services:Slow:?SOAP uses XML format that must be parsed to be read. It defines many standards that must be followed while developing SOAP applications. So it is slow and consumes more bandwidth and resources.WSDL dependent:?SOAP uses WSDL and does not have any other mechanism to discover the service.RESTful Web Services:REST stands for Representational State Transfer. REST is an architectural style, not a protocol.Advantages of RESTful Web Services:Fast:?RESTful Web Services are fast because there is no strict specification like SOAP. It consumes less bandwidth and resources.Language and Platform independent:?RESTful web services can be written in any programming language and executed in any platform.Permits different data formats:?Restful web service permits different data formats such as Plain Text, HTML, XML, and PARE SOAP vs REST Web Services:SNSOAP Web ServicesRESTful Web Services1.SOAP is a protocol.REST is an architectural style.2.SOAP stands for Simple Object Access Protocol.REST stands for Representational State Transfer.3.SOAP can’t use REST because it is a protocol.REST can use SOAP web services because it is a concept and can use any protocol like HTTP, SOAP.4.SOAP uses interfaces to expose business logic.REST uses URI to expose business logic.5.SOAP defines standards to be strictly followed.REST does not define too many standards like SOAP.6.SOAP requires more bandwidth/resources REST requires less bandwidth and resources than SOAP.7.SOAP defines its own security.REST web services inherit security measures from the underlying transport.8.SOAP permits an XML data format only.REST permits different data format such a Plain text, HTML, XML, JSON, etc.9.SOAP is less preferred than REST.REST more preferred than SOAP.More on Restful Web ServiceREST is used to build Web services that are lightweight, maintainable, and scalable in nature. A service which is built on the REST architecture is called a RESTful service. The underlying protocol for REST is HTTP, which is the basic web protocol. REST stands for REpresentational State Transfer.Unlike SOAP, REST is not constrained to XML, but instead can return XML, JSON, YAML or any other format depending on what the client requests. And unlike RPC, users aren’t required to know procedure names or specific parameters in a specific order.4819650391160000Restful Web Service can be DONE by:PHPPython with FlaskNodeJS and ExpressJava and SpringC# and .NET 2. The CS Linux system Every CS student can have:a CS Linux accounta MySQL database accountIf you haven’t done so then email Aram (agajania@cs.newpaltz.edu) ASAP and ask him to create accounts for you.+ CS Web Server address (used for PUTTY or SSH, WIN_SCP, to remotely access or transfer files to/from your CS account at CS server to your computer, download PUTTY and WINSCP if needed): wyvern.cs.newpaltz.edu+ web space (for your web pages) is whatever inside the folder named WWW (all capitalized) inside your CS account. You’ll need to create this folder if you haven’t done so. Must set the permission to 755: chmod 755 WWW+ when you run the web server programs the ERRORS are listed at a (shared with all other CS users) log, you see it by this command:tail /var/log/httpd24/error_log+ if you need to edit codes while accessing your account remotely you can use this TEXT editor in the CS system:jpico index.htm+ get to your MySQL account: you must login into your CS account first, then you can get to MySQL from there by the following:mysql -h localhost -p (click enter, then type in your MySQL password which is different from the CS password)USE YourUseName_db; (a SQL command to open/unlock your database, must do this before doing anything else with database, YourUserName is the students NPCUID. The database name is the username followed by "_db")To get out of MySQL account and get BACK to your CS account you can type “quit” or “CTRL+C”+ If you COPY some code/text from a windows-based file like this one and paste it in a file in your account which is a Linux-based one you may need convert a Windows text file to Linux/Unix one. You can use the following command:dos2unix *in the folder which contain the files at the server (you’ll need to run this at the shell level, via putty or ssh):BASIC LINUX COMMANDSls=> to see the names of files and folders in your accountmkdir PDS=> to create a folder/directory named PDSls -l=> to see a list of files and folders with all detailscd PDS=> to go inside folder PDSls=> to see the names of files and folders in the current foldermkdir 01=> to create a folder/directory named 01cd 01=> to go inside folder 01ls=> to see the names of files and folders in the current folderrm a.txt => to delete the file a.txtcp a.txt b.txt => to make a copy of file a.txt, the new copy is named b.txtmv a.txt X/b.txt => to move file a.txt to folder X, the file also has a new name b.txtcd ..=> to go out of the current folder (up one level) 3. SET-UP the web space for WEB SERVICES at the CS Linux systemStep 1: use PUTTY to login to your CS accountStep 2: (skip this if you already have a WWW folder created and set up) create a web space (for your web pages) i.e. a folder named WWW (all capitalized) inside your CS account. mkdir WWWMust set the permission to 755:chmod 755 WWWTEST: to see if you have it done correctly, go to a web browser such as Chrome and type in the following web address:cs.newpaltz.edu/~YourUserName/ To keep your web space organized you may want to create a sub-folder inside this WWW folder for our next projects:cd WWWTo keep your web space organized you may want to put a new sub-folder inside the folder “learn” (previously created in the Web Workshop) in this WWW folder for our Web Services projects:cd learnCreate a sub-folder, for example “se1” (web service example 1):mkdir se1Must set the permission to 755:chmod 755 se1TEST: to see if you have it done correctly, go to a web browser such as Chrome and type in the following web address:cs.newpaltz.edu/~YourUserName/learn/se1 4. Example A (a simple Web Service without Database which returns RAW data )4.1 SERVER side (to HOST/PROVIDE the web service)Usually a Web Service has two parts: (i) part A (api.php file) is responsible for dealing with HTTP GET requests and delivering output in JSON format to the user; (ii) part B (data.php file) fetches the price of particular product. For simplicity, we are fetching the price from an array instead of the database. Create a subfolder “se1” under WWW and two .php files:/se1/Web Service"api.php"Part A: create answer"data.php"Part B: data api.php<?phpheader("Content-Type:application");require "data.php";if(!empty($_GET['name'])){$name=$_GET['name'];$price = get_price($name);if(empty($price)){response(NULL);}else{response($price);}}else{response(NULL);}function response($data){header("HTTP/1.1 ");echo $data;}?>data.php<?phpfunction get_price($name){$products = ["ball"=>7,"car"=>5,"hat"=>10];foreach($products as $product=>$price){if($product==$name){return $price;break;}}}?>4883150257810DATA at the SERVER side and is hardcoded in the server program (without database):Name of the ItemThe Price of the Item“product”“price”ball7car5hat1000DATA at the SERVER side and is hardcoded in the server program (without database):Name of the ItemThe Price of the Item“product”“price”ball7car5hat104.2 CLIENT side (to REQUEST/CALL the web service)The CLIENT for a Web Service can be a web browser, phone app, a program on your PC, …. You’ll need to use a client to TEST your codes for a web service. Here is a DEMO example:The REQUEST via WWW from the client to the server: [ QUESTION =”what is the price of the car ?” ] RESPONSE/RESULT which goes back to the client from the server [or ANSWER = ‘5’]:5This [ANSWER = 5] is the raw information, without any tags. Users may not understand what it means. 5. Example B (a simple Web Service without Database which returns multiple items in JSON)5.0 DATA in JSON formatJSON:?JavaScript?Object?Notation. It is a syntax for storing and exchanging data. JSON adds meaning to the data and is human-readable text to transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value). It is a very common data format. Even though it was derived from JavaScript, JSON is a language-independent data format. EXAMPLE:RAW data:John 35 AlbanySteve 28 HudsonJSON format:{"students":[ {"name":"John",?"age":"35", "city":"Albany"}, {"name":"Steve",?"age":"28", "city":"Hudson"},]}XML format:<students> <student> <name>John</name>?<age>35</age> <city>Albany</city> </student> <student> <name>Steve</name>?<age>28</age> <city>Hudson</city> </student></students>5.1 SERVER side (to HOST/PROVIDE the web service)This Web Service has two parts: (i) part A (api.php file) is responsible for dealing with HTTP GET requests and delivering output in JSON format to the user; (ii) part B (data.php file) fetches the price of particular product. For simplicity, we are fetching the price from an array instead of the database. Create a subfolder “se2” under WWW and two .php files:/se2/Web Service"api.php"Part A: create answer"data.php"Part B: dataapi.php<?phpheader("Content-Type:application/json");require "data.php";if(!empty($_GET['name'])){$name=$_GET['name'];$price = get_price($name);if(empty($price)){response(200,"Product Not Found",NULL);}else{response(200,"Product Found",$price);}}else{response(400,"Invalid Request",NULL);}function response($status,$status_message,$data){header("HTTP/1.1 ".$status);$response['status']=$status;$response['status_message']=$status_message;$response['data']=$data;$json_response = json_encode($response);echo $json_response;}?>DATA at the SERVER side which is still hardcoded in the server program (without database):Name of the ItemThe Price of the Item“product”“price”ball7car5hat10data.php<?phpfunction get_price($name){$products = ["ball"=>7,"car"=>5,"hat"=>10];foreach($products as $product=>$price){if($product==$name){return $price;break;}}}?>5.2 CLIENT side (to REQUEST/CALL the web service)The CLIENT for a Web Service can be a web browser, phone app, a program on your PC, …. You’ll need to use a client to TEST your codes for a web service. Here is a DEMO example:The REQUEST via WWW from the client to the server: [ QUESTION =”what is the price of the ball ?” ] RESPONSE/RESULT should be:{"status":200,"status_message":"Product Found","price":7}This is the information in JSON format. [ANSWER =”ok, found it, price is 7”]6. Example C (a simple Web Service with Database which returns multiple items in JSON)6.0 CREATE a TABLE in MySQL database You can use a database to store data, in this case: the user name and password of users who can have access to certain things in your website. We have MySQL databse at our CS system. If you don’t have a MySQL account yet then email Aram (agajania@cs.newpaltz.edu) ASAP and ask him to create an account for you. Once, you have your MySQL account, you can access your MySQL database. a) You must login into your CS account first (using the your regular college account username and password)b) Then, you can get to MySQL from there by the following:mysql -h localhost –p (click enter, then type in your MySQL password which is different from your college password)USE YourUseName_db; (a SQL command to open/unlock your database, must do this before doing anything else with database, YourUserName is the students NPCUID. The database name is the username followed by "_db")c) Open/unlock your MySQL database (must do this before doing anything else with database, YourUserName is the students NPCUID. The database name is the username followed by "_db")USE YourUseName_db; After that, you can work (create, read, write tables) with your MySQL database. In this case: d) manually CREATE a TABLE called “items” which store product info with two columns (product, price) in your MySQL database using the following SQL statement:“product”“price”CREATE TABLE items(product CHAR(30) NOT NULL, price CHAR(10)NOT NULL,PRIMARY KEY(product));e) Insert/add data (USERNAME and PASSWORD) to the table. This is done “manually” at the command-line level.INSERT INTO items(product,price)VALUES('ball','7'),('car','5'),('hat','10');MySQL table:“items”“product”“price”ball7car5hat10f) You can check to see the contents of the table itemsSELECT * FROM items;You should see something like this: 6.1 SERVER side (to HOST/PROVIDE the web service)This Web Service has two parts: (i) part A (api.php file) is responsible for dealing with HTTP GET requests and delivering output in JSON format to the user; (ii) part B (data.php file) fetches the price of the given product (in $name) from a MySQL table. Create a subfolder “se3” under WWW and two .php files:Web Service/se3/Part A: answer"api.php"stays the samePart B: data"data.php"must change code to connect to MySQLapi.php<?phpheader("Content-Type:application/json");require "data.php";if(!empty($_GET['name'])){$name=$_GET['name'];$price = get_price($name);if(empty($price)){response(200,"Product Not Found",NULL);}else{response(200,"Product Found",$price);}}else{response(400,"Invalid Request",NULL);}function response($status,$status_message,$data){header("HTTP/1.1 ".$status);$response['status']=$status;$response['status_message']=$status_message;$response['data']=$data;$json_response = json_encode($response);echo $json_response;}?>DATA at the SERVER side: in a MySQL table called “items” as the following:“product”“price”ball7car5hat10data.php<?phpfunction get_price($name){ /* Database INFO */$servername = "localhost";$username = "YourUserName";$password = "YourMySQLpassword";$dbname = "YourUserName_db"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT price FROM items WHERE product = '$name'"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $price = $row["price"]; } } else { $price = null; } $conn->close(); return $price;}?>6.2 CLIENT side (to REQUEST/CALL the web service)The CLIENT for a Web Service can be a web browser, phone app, a program on your PC, …. You’ll need to use a client to TEST your codes for a web service. Here is a DEMO example:The REQUEST via WWW from the client to the server: [ QUESTION =”what is the price of the ball ?” ] RESPONSE/RESULT should be:{"status":200,"status_message":"Product Found","price":7}This is the information in JSON format. [ANSWER =”found it, price is 7”]7. Example D (Web Service with Database, multiple parameters and multiple returns in JSON)We use the same MySQL table as in the last (#3) example. 7.1 SERVER side (to HOST/PROVIDE the web service)This Web Service has two parts: (i) part A (api.php file) is responsible for dealing with HTTP GET requests and delivering output in JSON format to the user; (ii) part B (data.php file) use the $name to fetch its $price in a MySQL table. Create a subfolder “se4” under WWW and two .php files:Web Service/se4/Part A: answer"api.php"must change code to take in TWO parametersPart B: data"data.php"must change code to check if both given data are in the MySQL tableapi.php<?phpheader("Content-Type:application/json");require "data.php";if(!empty($_GET['name']) and !empty($_GET['price'])){$name=$_GET['name'];$price=$_GET['price'];$r = get_price($name,$price);if(empty($r)){response(200,"Product Not Found",NULL);}else{response(200,"Product Found",$r);}}else{response(400,"Invalid Request",NULL);}function response($status,$status_message,$data){header("HTTP/1.1 ".$status);$response['status']=$status;$response['status_message']=$status_message;$response['data is correct']=$data;$json_response = json_encode($response);echo $json_response;}?>DATA at the SERVER side: in a MySQL table called “items” as the following:“product”“price”ball7car5hat10data.php<?phpfunction get_price($name, $price){ /* Database INFO */$servername = "localhost";$username = "YourUserName";$password = "YourMySQLpassword";$dbname = "YourUserName_db"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT price FROM items WHERE product = '$name'"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $p = $row["price"]; } } else { $p = null; } $conn->close();if ($p == $price) { return "true"; }else { return "false"; }}?>7.2 CLIENT side (to REQUEST/CALL the web service)The CLIENT for a Web Service can be a web browser, phone app, a program on your PC, …. You’ll need to use a client to TEST your codes for a web service. Here is a DEMO example:The REQUEST via WWW from the client to the server: [ QUESTION =”Is 10 the price of the hat ?” ] RESPONSE/RESULT should be:{"status":200,"status_message":"Product Found","data is correct": "true": }This is the information in JSON format. [ANSWER =”YES”] 8. DEBUGING your programsIf you use PHP then the errors are displayed on the web browser/page when you test the web service. It’s completely normal if first you end up with errors. Developing software is about fixing/debugging errors. However, developing web-based systems including this simple example is more challenging because when you TEST your program you’ll need to do it in a WEB BROWSER which is at the CLIENT site (your laptop) while the whole chain of programs involved (start with HTML form=>CGI program => MySQL table => HTML response/output back to the CLIENT site) runs at both SERVER and CLIENT sites and the system won’t tell you exactly where the errors are. Usually only “Internal Sever Error”.Here is the list of things you may like to check:Check if all file names (also those in the codes) are EXACTLY as in the instructions ? Remember it’s case sensitive.If all the folders and files inside have the right permissions (755 would be ok) ?If you have already created the table UserPass table in your MySQL account ?If you have replaced "YourUserName","YourMySQLpassword","YourUserName_db" in the SERVER programs with your OWN account info ?Have you run this command dos2unix * yet ?You may also want to copy-and-paste the codes for the two CGI programs again. First copy the code in this file to a text editor like NOTEPAD (not notepad++), then select the code in there and copy into the file in your Linux account.Get more INFO about where your errors are. The errors are usually are listed in /var/log/httpd24/error_log but this is shared by all users/accounts and can get very crowded when many users use/test the webpages at the same time. You can read just the parts which relate to your account by:Run this command in the putty/ssh window:tail -f /var/log/httpd24/error_log | grep YourUserName(replace YourUserName with your user name in the CS/New Paltz system) it will wait until you run the testTest/run the webpage/form which calls the CGI programGo back to the putty/ssh window and read carefully the messages there 9. ReferencesWS with PHP: with Python with NodeJS and Express: with .NET with Java ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download