Lecture 17: Web Service and post

Slides:



Advertisements
Similar presentations
1 Configuring Internet- related services (April 22, 2015) © Abdou Illia, Spring 2015.
Advertisements

1 Configuring Web services (Week 15, Monday 4/17/2006) © Abdou Illia, Spring 2006.
Hypertext Transfer Protocol Kyle Roth Mark Hoover.
HTTP Hypertext Transfer Protocol. HTTP messages HTTP is the language that web clients and web servers use to talk to each other –HTTP is largely “under.
CPSC 441: FTP & SMTP1 Application Layer: FTP & Instructor: Carey Williamson Office: ICT Class.
Hypertext Transfer Protocol Information Systems 337 Prof. Harry Plantinga.
HTTP Overview Vijayan Sugumaran School of Business Administration Oakland University.
RESTful Web Development With Nodejs and Express. REST Stands for REpresentational State Transfer Has the following constraints: ◦Client-Server ◦Stateless.
Web server and web browser It’s a take and give policy in between client and server through HTTP(Hyper Text Transport Protocol) Server takes a request.
J2EE Web Fundamentals Lesson 1 Introduction and Overview
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
JavaScript & jQuery the missing manual Chapter 11
Web application architecture
Google Cloud Messaging for Android (GCM) is a free service that helps developers send data from servers to their Android.
GET Examples – db.org/sops/3/experimental_conditions/55http://seek.sysmo- db.org/sops/3/experimental_conditions/55 –
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Chapter 6 Server-side Programming: Java Servlets
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Topics Sending an Multipart message Storing images Getting confirmation Session tracking using PHP Graphics Input Validators Cookies.
Appendix E: Overview of HTTP ©SoftMoore ConsultingSlide 1.
Internet Applications (Cont’d) Basic Internet Applications – World Wide Web (WWW) Browser Architecture Static Documents Dynamic Documents Active Documents.
Overview of Servlets and JSP
Slides based on Carey Williamson’s: FTP & SMTP1 File Transfer Protocol (FTP) r FTP client contacts FTP server at port 21, specifying TCP as transport protocol.
Spell Checker web service (you build a web client that interacts with the service) The client uses a servlet class and a JSP page. The user passes information.
Web Services Essentials. What is a web service? web service: software functionality that can be invoked through the internet using common protocols like.
CSE 154 LECTURE 18: FORMS AND UPLOADING FILES. Exercise: Baby name web service JSON Modify our babynames.php service to produce its output as JSON. For.
Setting Up First, install tomcat and axis as described elsewhere. Then copy Tomcat into a second folder. –I’ve named mine jakarta- tomcat-server and jakarta-
Lecture # 1 By: Aftab Alam Department Of Computer Science University Of Peshawar Internet Programming.
David Hatten Developer, UrbanCode 17 October 2013
Tiny http client and server
User-Written Functions
CS 330 Class 7 Comments on Exam Programming plan for today:
Node.js Express Web Applications
CSE 154 Lecture 24: JSON.
>> PHP: Form Processing
Intro to PHP & Variables
Network Programming Lecture 4: Message Posting
Week 04 (Final Project) Node.js Week 04 (Final Project)
Express with Node L. Grewe. Feature 1 Project structure.
Ashish Pandit IT Architect, Middleware & Integration Services
Testing REST IPA using POSTMAN
Lecture 19: Forms and uploading files
WEB API.
(Includes setup) FAQ ON DOCUMENTS (Includes setup)
Hypertext Transfer Protocol
Configuring Internet-related services
Abel Sanchez, John R. Williams
Hypertext Transfer Protocol
Web Programming Language
Web Service Installation and Configuration
Lecture 16: Writing Your Own Web Service
CSc 337 Lecture 27: Cookies.
Hypertext Transfer Protocol
Lecture 12: The Fetch Api and AJAx

Lecture 12: The Fetch Api and AJAx
CSc 337 Lecture 1: post.
Lecture 14: JSON and Web SERVICES
Generate Header & URL Install PostMan for Chrome (looks like a man with a jetpack) Under the auth tab, set it to basic Put in the admin username and password.
Running a Java Program using Blue Jay.
Chengyu Sun California State University, Los Angeles
(Includes setup) FAQ ON DOCUMENTS (Includes setup)
Hypertext Transfer Protocol
CSc 337 Lecture 18: post.
Chengyu Sun California State University, Los Angeles
Lecture 15: Writing Your Own Web Service
Restful APIs 101 Laura
CSc 337 Lecture 25: Cookies.
How To Repair Outlook Express Inbox.dbx File After Crash.
Presentation transcript:

Lecture 17: Web Service and post CSc 337 Lecture 17: Web Service and post

Reading from a file file = fs.readFileSync(file_name, 'utf8'); You can read from a file with the above code. Returns the contents of the file as a string.

Exercise Read data from a file called books.txt Make sure books.txt is in the same folder as your web service Output JSON that that contains a list of all books in the file. Each list item should contain the book's name, author, category, year and price as individual items.

Exercise - part 2 Add a parameter to your service so that when the user supplies the parameter books with a value of a category name your service will only output books in that category. If the user doesn't supply a parameter your service should produce all books. http://localhost:3000?books=computer

Exercise - part 3 If there are no books that are in the category that the user supplies have your service return a 410 status and a message about the category not being found. Set the status with the following code: res.status(410);

GET vs POST Two different types of requests you can send to a web service: GET - asks the server for data POST - sends data to the server Why can't POST requests work the same way as GET requests?

Dealing with a POST request in a service app.post('/', function (req, res) { res.header("Access-Control-Allow-Origin", "*"); res.send('Hello world'); }); - instead of using app.get use app.post - Send response as usual

Making a POST request from the client fetch(url, {method : 'POST'}) .then(checkStatus) .then(function(responseText) { }) .catch(function(error) { }); } Add a second parameter to fetch specifying the method There are many methods POST, PUT, PATCH, DELETE, … Get is the default method

Sending parameters Send parameters as JSON! By default parsing is really messy so we will install a helpful package - body-parser Run this on the command line in your code directory: npm install body-parser

Sending Parameters const message = {name: "Allison", email: "aeobourn@cs.arizona.edu"}; const fetchOptions = { method : 'POST', headers : { 'Accept': 'application/json', 'Content-Type' : 'application/json' }, body : JSON.stringify(message); } fetch(url, fetchOptions) .then(checkStatus) Add a stringified version of the JSON you want to send to an object containing the other options and send that.

Dealing with POST parameters on the server const bodyParser = require('body-parser'); const jsonParser = bodyParser.json(); app.post('/', jsonParser, function (req, res) { const name = req.body.name; res.send('Hello, ' + name); }); Accessing parameters is similar to with a get request except you need to access req.body instead of req.query

Prefilght CORS error The code on the previous slide produces an error because it is accepting complex parameter content and Allow-Access-Control-Origin isn't set. Adding this line in where we usually do doesn't fix this. Add the code below instead: app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); });