tcs.newpaltz.edu/.../web/notes/note-02-webservices.docx · web viewc# and .net 2. 2. the cs linux...

42
Page 1 of 42 Web Services GOALS: Advanced and intensive training Workshop on Web Services. Address full stack web programming: Client+Server+Database At 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: 1. What is a Web Service ? 2. The CS Linux system (same as in the Web Workshop) 3. SET-UP the web space for WEB SERVICES on the CS Linux server 4. Example A (a simple Web Service without Database which returns RAW data) 5. Example B (a simple Web Service without Database which returns multiple items in JSON) 6. Example C (a simple Web Service with Database which returns multiple items in JSON) 7. Example D (a simple Web Service with Database which returns multiple items in JSON) 8. DEBUGING your programs 9. References 1. What is a Web Service ? CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Upload: others

Post on 02-Dec-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 1 of 34

Web Services

GOALS: Advanced and intensive training Workshop on Web Services. Address full stack web programming: Client+Server+Database At 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:1. What is a Web Service ?2. The CS Linux system (same as in the Web Workshop)3. SET-UP the web space for WEB SERVICES on the CS Linux server4. Example A (a simple Web Service without Database which returns RAW data)5. Example B (a simple Web Service without Database which returns multiple items in JSON)6. Example C (a simple Web Service with Database which returns multiple items in JSON)7. Example D (a simple Web Service with Database which returns multiple items in JSON)8. DEBUGING your programs9. References

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).

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 2: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 2 of 34

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 amazon.com. ... 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:

1. SOAP Web Services2. RESTful Web Services

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 3: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 3 of 34

SOAP (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 JSON.

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 4: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 4 of 34

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 5: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 5 of 34

COMPARE SOAP vs REST Web Services:

SN SOAP Web Services RESTful Web Services

1. 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.

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 6: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 6 of 34

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 7: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 7 of 34

Rest ful Web Service can be DONE by:

PHP Python with Flask NodeJS and Express Java and Spring C# and .NET

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 8: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 8 of 34

2. The CS Linux system

Every CS student can have: a CS Linux account a MySQL database account

If you haven’t done so then email Aram ([email protected]) 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

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 9: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 9 of 34

+ 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 COMMANDS

$ ls => to see the names of files and folders in your account

$ mkdir PDS => to create a folder/directory named PDS

$ ls -l => to see a list of files and folders with all details

$ cd PDS => to go inside folder PDS

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 10: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 10 of 34

$ ls => to see the names of files and folders in the current folder

$ mkdir 01 => to create a folder/directory named 01

$ cd 01 => to go inside folder 01

$ ls => to see the names of files and folders in the current folder

$ rm a.txt => to delete the file a.txt

$ cp a.txt b.txt => to make a copy of file a.txt, the new copy is named b.txt

$ mv a.txt X/b.txt => to move file a.txt to folder X, the file also has a new name b.txt

$ cd .. => to go out of the current folder (up one level)

3. SET-UP the web space for WEB SERVICES at the CS Linux system

Step 1: use PUTTY to login to your CS account

Step 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 WWW

TEST: 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 WWW

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 11: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 11 of 34

To 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 learn

Create a sub-folder, for example “se1” (web service example 1):$ mkdir se1

Must set the permission to 755:$ chmod 755 se1

TEST: 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)

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 12: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 12 of 34

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)

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 13: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

DATA at the SERVER side and is hardcoded in the server program (without database):

Name of the Item The Price of the Item“product” “price”

ball 7car 5hat 10

Page 13 of 34

{header("HTTP/1.1 ");

echo $data;}

?>

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

data.php

<?php

function get_price($name){

$products = ["ball"=>7,"car"=>5,"hat"=>10

];

foreach($products as $product=>$price){

if($product==$name){

return $price;break;

}}

}

?>

Page 14: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 14 of 34

4.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 ?” ]

http://cs.newpaltz.edu/~phamh/basics/web/se1/api.php?name=car

The RESPONSE/RESULT which goes back to the client from the server [or ANSWER = ‘5’]:

5

This [ANSWER = 5] is the raw information, without any tags. Users may not understand what it means.

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 15: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 15 of 34

5. Example B (a simple Web Service without Database which returns multiple items in JSON)

5.0 DATA in JSON format

JSON: 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 Hudson

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 16: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 16 of 34

JSON 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: data

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 17: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 17 of 34

api.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;

}

?>

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 18: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 18 of 34

DATA at the SERVER side which is still hardcoded in the server program (without database):

Name of the Item The Price of the Item“product” “price”

ball 7car 5hat 10

data.php

<?php

function get_price($name){

$products = ["ball"=>7,"car"=>5,"hat"=>10

];

foreach($products as $product=>$price){

if($product==$name){

return $price;break;

}}

}

?>

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 19: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 19 of 34

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 ?” ]

http://cs.newpaltz.edu/~phamh/basics/web/se2/api.php?name=ball

The 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”]

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 20: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 20 of 34

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 21: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 21 of 34

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 ([email protected]) 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)

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 22: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 22 of 34

$ 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”

ball 7car 5hat 10

f) You can check to see the contents of the table items

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 23: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 23 of 34

SELECT * 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 MySQL

api.php

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 24: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 24 of 34

<?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;

}?>

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 25: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 25 of 34

DATA at the SERVER side: in a MySQL table called “items” as the following:

“product” “price”ball 7car 5hat 10

data.php

<?php

function 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"];

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 26: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 26 of 34

} } 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 ?” ]

http://cs.newpaltz.edu/~phamh/basics/web/se3/api.php?name=ball

The 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”]

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 27: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 27 of 34

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.

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 28: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 28 of 34

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 table

api.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);}

}

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 29: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 29 of 34

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”ball 7car 5hat 10

data.php

<?php

function get_price($name, $price){

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 30: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 30 of 34

/* 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";

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 31: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 31 of 34

}

}

?>

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 ?” ]

http://cs.newpaltz.edu/~phamh/basics/web/se4/api.php?name=hat&price=10

The RESPONSE/RESULT should be:

{"status":200,"status_message":"Product Found","data is correct": "true": }

This is the information in JSON format. [ANSWER =”YES”]

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 32: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 32 of 34

8. DEBUGING your programs

If 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 ?

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 33: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 33 of 34

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:

a) 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 testb) Test/run the webpage/form which calls the CGI programc) Go back to the putty/ssh window and read carefully the messages there

9. References

WS with PHP:https://shareurcodes.com/blog/creating%20a%20simple%20rest%20api%20in%20phphttps://www.leaseweb.com/labs/2015/10/creating-a-simple-rest-api-in-php/https://www.allphptricks.com/create-and-consume-simple-rest-api-in-php/https://www.phpflow.com/php/create-php-restful-api-without-rest-framework-dependency/

WS with Pythonhttps://codeburst.io/this-is-how-easy-it-is-to-create-a-rest-api-8a25122ab1f3

WS with NodeJS and Express:https://medium.com/@onejohi/building-a-simple-rest-api-with-nodejs-and-express-da6273ed7ca9

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham

Page 34: Tcs.newpaltz.edu/.../web/notes/Note-02-WebServices.docx · Web viewC# and .NET 2. 2. The CS Linux system Every CS student can have: a CS Linux account a MySQL database account If

Page 34 of 34

WS with .NEThttps://www.guru99.com/restful-web-services.html

WS with Java https://terasolunaorg.github.io/guideline/5.3.0.RELEASE/en/ArchitectureInDetail/WebServiceDetail/REST.html

CS Note # 02 “CS Must-Have Skills” Dr. Hanh Pham