cisc 3120 c24: web api: passing arguments and parsing returns · •first, understand the format of...

32
CISC 3120 C24: Web API: Passing Arguments and Parsing Returns Hui Chen Department of Computer & Information Science CUNY Brooklyn College 5/7/2018 1 CUNY | Brooklyn College

Upload: others

Post on 26-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

CISC 3120

C24: Web API: Passing Arguments and Parsing

ReturnsHui Chen

Department of Computer & Information Science

CUNY Brooklyn College

5/7/2018 1CUNY | Brooklyn College

Page 2: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Outline

• Parsing arguments/data to Web server

• Parsing returned value/data from Web server

5/7/2018 CUNY | Brooklyn College 2

Web server

HTTP

JSON/XML objects

Web client

Path variables & request parameters

Page 3: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Passing Arguments via URL

• URL syntax

• scheme://authority[path][?query][#fragment]

5/7/2018 CUNY | Brooklyn College 3

Contains arguments

Path variables Query parameters(request parameters)

Page 4: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Passing Argument via Request Body

• Request with the HTTP POST method

5/7/2018 CUNY | Brooklyn College 4

POST /index.html HTTP/1.1

User-Agent: Java Web Client

Host: localhost:61235

Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

Connection: keep-alive

……

head

er

bod

y

Two blank lines

Request method, URN, protocol version

Contains arguments(request parameters)

Page 5: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Passing Arguments

• Use java.net.HttpURLConnection

• Path variables

• Request parameters

• In URL (with the HTTP GET method)

• In HTTP request Body (with the HTTP POST method)

• Note

• Libraries and 3rd Party APIs may provide convenient methods or mechanisms

• Example: jdk.incubator.http.HttpRequest

• “incubator” are Java features that are under development

• Example: org.apache.http.HttpRequest

• Apache HTTP client API

5/7/2018 CUNY | Brooklyn College 5

Page 6: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Path Variables: Example

• Use String.format(…) method

• URL-encode strings for compatibility

• Not the entire URL!

• java.net.URLEncoder

• Example• final static String WEB_API_FMT = “http://example.com/%s/%d”;

• String itemName = “blue moon”; int type = 5;

• String urlResource = String.format(WEB_API_FMT, URLEncoder.encode(itemName, StandardCharsts.UTF_8.name()), type);

• Example: the Address-auto-fill-by-zipcode example

5/7/2018 CUNY | Brooklyn College 6

Page 7: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Request Parameters

• General guideline for preparing the parameters

• Form key-value pairs, separate with “&”

• Send the key-value pairs to the server

• Examples

• Using a Map data structure

• A map data structure is a list of key-value pair

• URL-encode and key and value

5/7/2018 CUNY | Brooklyn College 7

Page 8: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

URL Query Component

• URL syntax

• scheme://authority[path][?query][#fragment]

• Concatenate the prepared parameters (name-value pairs) with the URL

• Example:

• apiResource = url + “?” + preparedQuery;

5/7/2018 CUNY | Brooklyn College 8

Query parameters(request parameters)

Page 9: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Parameters in Request Body

• Request with the HTTP POST method

5/7/2018 CUNY | Brooklyn College 9

POST /index.html HTTP/1.1

User-Agent: Java Web Client

Host: localhost:61235

Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

Connection: keep-alive

……

head

er

bod

y

Two blank lines

Request method, URN, protocol version

Contains arguments(request parameters)

Page 10: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Parameters in Request Body: HttpURLConnection

• HttpURLConnection or HttpsURLConnection

• Example:

• given HttpURLConnection conn = … and prepared query string in query, do,

conn.setDoOutput(true);

try (OutputStream out = conn.getOutputStream()) {

out.write(query.getBytes());

}

5/7/2018 CUNY | Brooklyn College 10

Page 11: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Which Approach to Use?

• Which method to use in your client?

• Depends on the design and implementation of Web API

• Some uses path variables

• Some only allows GET method (query in URL)

• Some only allows POST method (query in REQUEST body)

• Some support both

5/7/2018 CUNY | Brooklyn College 11

Page 12: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Questions?

• Passing arguments/data to the Web server

• Examine Web API determine which one to use

• Path variables

• Request parameters

5/7/2018 CUNY | Brooklyn College 12

Page 13: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Return Value in the Response

• Parsing return values in the response message

• JSON or XML

• Only discuss JSON

5/7/2018 CUNY | Brooklyn College 13

Web client Web server

HTTP

JSON/XML

Page 14: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

JSON

• A simple data exchange format

• Easy for humans to read

• Easy for programs to process

• https://www.json.org/

• Two structures

• JSON object

• JSON array

5/7/2018 CUNY | Brooklyn College 14

Page 15: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

JSON Object

• An unordered set of name-value pairs

• Enclosed in a pair of braces “{“ and “}”.

• Name and value separated by a “:” in a name-value pair

• Name-pairs are separated by “,”

• Example

{

“country”: “US”,

“state”: “NY”,

“city”: “BROOKLYN”

}

5/7/2018 CUNY | Brooklyn College 15

Page 16: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

JSON Array

• An unordered collection of values

• Enclosed in a pair of brackets “[“ and “]”

• Values are separated by “,”

• Examples

[

“Brooklyn College”,

“Hunger College”,

“City College”,

“Lehman College”

]

5/7/2018 CUNY | Brooklyn College 16

Page 17: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

JSON Value

• What can be a value?

• String: quoted character sequence

• e.g., “Brooklyn College”

• Number: an integer, or a float pointing number

• e.g., 3, 3.14, 3.14e0, 0.314e1, 0.314e+1, 31.4e-1

• JSON object, i.e., JSON objects can be nested with JSON objects or arrays

• JSON array, i.e., JSON arrays can be nested with JSON objects or arrays

• true

• false

• null

5/7/2018 CUNY | Brooklyn College 17

Page 18: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

JSON Object: Example

{

“club”: “Alpha Beta Gamma”,

“president”: {“name”:”Ben Jefferson”, “gpa”:3.8},

“vice president”: null,

member: [

{“name”:”Jane Doe”, “gpa”:4.0, “graduated”: false },

{“name”:”John Doe”, “gpa”:4.0, “graduated”: true},

]

}

5/7/2018 CUNY | Brooklyn College 18

Page 19: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Dealing with JSON Objects and Arrays

• Many APIs and libraries

• https://www.json.org/

• In this course,

• JSONP by Oracle

• https://javaee.github.io/jsonp/

• Examples

• https://javaee.github.io/jsonp/getting-started.html

5/7/2018 CUNY | Brooklyn College 19

Page 20: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Questions?

• Dealing with JSON array/object in HTTP response?

5/7/2018 CUNY | Brooklyn College 20

Page 21: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Full Example in Application

• Full Web client example

• Charting multiple equities price over time (using Alpha Vantage Web API)

• First, understand the format of the JSON object that Alpha Vantage API returns

• Second, determine to create JSON object or array

• Third, determine Java data structure

5/7/2018 CUNY | Brooklyn College 21

JSON (object, array, nested?)

Java Data Structures (set, map, vector, sorted,

unsorted?)

Parsing

Page 22: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Main Steps in the Web Client

• Create a URL connection

• Prepare a HTTP request

• Send request

• Read response

• Disconnect

5/7/2018 CUNY | Brooklyn College 22

Page 23: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Create URL Connection

• URL’s openConnection() method

• Determine if it is HttpURLConnection or HttpsURLConnection

• Can only be one of the two, if Web

5/7/2018 CUNY | Brooklyn College 23

Page 24: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Prepare HTTP Request

• Request method: GET or POST or …?

• Path variables and request parameters

• Additional items

• Some additional request header fields: “content-type”, “user-agent”

• Network timeout: connect or read timeout, use default or set a desired value

• Use Web cookie: send cookie in the request?

• Handling redirection

5/7/2018 CUNY | Brooklyn College 24

Page 25: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Send Request

• Use connect() method

• of the HttpURLConnection or the HttpsURLConnection object

• As side effect of any one of the three methods of the connection object

• getResponseCode()

• getInputStream()

• getOutputStream()

5/7/2018 CUNY | Brooklyn College 25

Page 26: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Read Response

• Error or not?

• If error, read the error message via the stream from getErrorStream()

• Parse JSON array/object

5/7/2018 CUNY | Brooklyn College 26

Page 27: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Disconnect

• Each HttpURLConnection instance is used to make a single request

• However, the underlying network connection to the HTTP server may be shared by other instances.

• Calling the close() methods on the InputStream or OutputStream of an HttpURLConnection after a request may free network resources associated with this instance

• but has no effect on any shared persistent connection.

• Calling the disconnect() method may close the underlying socket if a persistent connection is otherwise idle at that time.

5/7/2018 CUNY | Brooklyn College 27

Page 28: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Questions?

• Concept of JSON?

• Full application example

5/7/2018 CUNY | Brooklyn College 28

Page 29: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

HTTP Cookie

• Also called Web Cookie, Browser Cookie, Cookie

• A small piece of data stored by the user agent sent from the Web server

• HTTP is stateless

• i.e., whenever the Server finishes sending the response, it forgets about the client

• Cookie is invented for the Web server to remember about a client

• Web server sends a cookie to the client (user agent)

• The client may choose to store the cookie and send it back with the next request to the server

5/7/2018 CUNY | Brooklyn College 29

Page 30: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

HTTP Cookie: Main Purposes

• Session management

• Example: login/logout, shopping cart, game score, anything else the server should remember about the client

• Personalization

• User preferences, themes, and other settings

• Tracking

• Recording and analyzing user behavior

5/7/2018 CUNY | Brooklyn College 30

Page 31: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Handling Cookies

• A few classes and interfaces in the java.net package,

• CookieHandler, CookieManager, CookiePolicy, CookieStore, and HttpCookie.

5/7/2018 CUNY | Brooklyn College 31

Page 32: CISC 3120 C24: Web API: Passing Arguments and Parsing Returns · •First, understand the format of the JSON object that Alpha Vantage API returns •Second, determine to create JSON

Questions?

• Concept about HTTP Cookie

5/7/2018 CUNY | Brooklyn College 32