Chapter 47 - Understanding REST and JSON Flashcards
What is a library?
- Prebuilt index of data that can be used instead of writing the equivalent from scratch
What are the attributes that define REST APIs?
- Client/server architecture - Uses API calls (E.g. HTTP requests) to access data on the REST server
- Stateless operation - Does not use information from previous API exchanges to influence future API exchanges
- Clear statement of cacheable and uncacheable - Marks data that should or should not be cached for better performance (if cached, the server does not have to retrieve a resource for the client after already retrieving it within a certain timeframe)
- Uniform interface
- Layered
- Code-on-demand
What are Simple Variables?
- Variables with a single data name and a single attributed value
What are Array Data Structures?
- Data Structure with a single data key but multiple attributed values
What are Object Data Structures?
- Data structures that contain multiple key:value pairs
- Sometimes called a Dictionary
Why do a lot of REST APIs use HTTP as their networking protocol?
- Very similar (e.g. both client/server, both stateless, both clearly mark cacheable and uncacheable data)
- REST APIs can also use URIs to specify in HTTP what data it needs to perform an action on
What is CRUD?
- An acronym for the four primary actions of an application:
- Create - Allows users to create variables and data structures at a server and set their initial values
- Read - Allows users to retrieve the current value of variables that exist at a server
- Update - Allows the client to update variables and data structures at a server
- Delete - Allows the client to delete variables from a server
List HTTP verbs and their corresponding CRUD terms
- POST - Create
- GET - Read
- PATCH/PUT - Update
- DELETE - Delete
What are the different parts of a URI?
HTTPS://google.com/path/to/resource?specific=query
- Before the :// - Protocol (Scheme)
- Immediately after the :// - The hostname of the server (authority)
- Immediately after the / - The path to the resource on the server
- Immediately after the ? - A query that specifies the variable that the client is querying at a given destination
What is JSON?
- JavaScript Object Notation
- Open standard file format and data interchange format that stores and transmits data in human readable text
- Data serialisation language
- Commonly used in REST APIs
- Whitespace is insignificant
What is a Data Serialisation Language?
- Provides a standardised method of representing data between programs. Useful when programs are written in different languages as they may interpret/represent variables differently
- An example is JSON, XML, YAML
- Sometimes known as a Data Modelling Language
What is XML?
- eXtensible Markup Language
- Data serialisation language
- A successor to HTML that improves on the ability for variables to be changed dynamically
- Commonly used in REST APIs and web pages
- Whitespace does not change the meaning of data
- key value pairs are written as <key> value </key>
What is YAML?
- YAML Ain’t Markup Language
- Data serialisation language
- Unlike XML focuses more on data model details than markup
- Commonly used in Ansible
- White space does change the meaning of data
- Files start with ‘—’
- Lists start with ‘-‘
- Key value pairs are defined as key:value
What is markup in the context of programming?
- The ability for a programming language to add more information to variables to define how they are displayed
JSON syntax rules
- Key:Value Pair - Every colon identifies one key:value pair with the key before the colon and the value after.
- Key - Text inside double quotes, before a colon that is used as a name that references a value
- Value - Item after the colon that represents the value of the key. These can be:
- string
- number
- null
- boolean
- Array
- Object - An unordered list of key:value pairs - Multiple Pairs - Separate each pair with a comma at the end of each pair apart from the last one
- Curly brackets - Begin and end a JSON object. A series of key:value pairs enclosed in curly brakcets. Python knows them as dictionaries.
- Square brackets - Begin and end a JSON array. A series of values (not key:value pairs) enclosed in square brackets. Python knows them as lists.
- Objects and arrays can be nested within each other
- Whitespace does not change the meaning of data
What are JSON data types?
- null- Represents the intentional lack of a value. Just the word null in no quotes.
- string - Text value. Surrounded by quotes.
- boolean - True/False value. Not surrounded by quotes.
- number - Numeric value. Not surrounded by quotes.
What are variables?
- A data container that stores a value
How many key:value pairs are in the below?
{
“Identity”:{
“Interface”:”G0/1”,
“Enabled”:true
}
}
- 3
- However, “Identity” is a key and then everything inside the second set of curly brackets is the value.
What are nested objects?
- Objects within objects
- An example is:
{
“Identity”:{
“Interface”:”G0/1”,
“Enabled”:true
}
}
True or False. In JSON, having a comma at the end of the last key:value pair in an object is fine.
False. There should only be a comma between key:value pairs and not after the last key:value pair.
What are HTTP response codes?
- 3 digit numbers that define the response from a HTTP server:
- 1xx - Informational (e.g. 102 - Processing)
- 2xx - Successful (e.g. 200 - OK, 201 - Created)
- 3xx - Redirection (e.g. 301 - Moved permanently)
- 4xx - Client error (e.g. 403 - Unauthorised, 404 - Not found)
- 5xx - Server error (e.g. 500 - Internal server error)