Lecture 9 Flashcards
Request/Response cycle, response status codes, GET and POST requests, APIs, making get requests, JSON response
what is DNS? (2)
1 domain name server
2 the process through which human-readable domain names (www.digicert.com) are translated into a computer-readable IP address (216.168. 246.55)
Explain the request response cycle
- computer makes a request from the server
- server processes the request
- server issues a response
Response status codes (4)
2xx - Success
3xx - Redirect (example we get directed to google.com without a www)
4xx - Client Error (your fault!)
5xx - Server Error (not your fault!)
what are GET and POST requests
GET is used to request data from a specified resource
POST request - Useful for writing data
what is a post request? + example
POST requests can transmit data of any type
Example of POST response is posting a photo on facebook.
API
(4)
1 Application programming interface
2 Allows you to get data from another application without needing to understand how the application works
3 Can often send data back in different formats
4 Examples of companies with APIs: GitHub, Spotify, Google
request module + syntax
allows you to send HTTP requests in Python
!pip install requests
requests get method (3) + syntax
1 retrieves information from the server using a given URL. The GET method sends the encoded user information appended to the page request.
2 The page and the encoded information are separated by the ‘?’ character.
3 not suitable for sending sensitive information
requests.get(url, params={key: value}, args)
ok method + syntax
returns True if status_code is less than 400, otherwise False
x.ok
where x is a variable containing a get request eg
x=requests.get(url)
.text method
returns the content of the response, in unicode. Basically, it refers to Binary Response content. (???)
headers ()
1 HTTP headers let the server know additional information about the reqeuest
2 All the headers are NOT case-sensitive
3 headers fields are separated by colon, key-value pairs.
ex: Headers = { “Authorization” : ”our_unique_secret_token” }
.status_code method
returns status code of a get request
what does the headers argument do in the response module?
import requests
url = “https://icanhazdadjoke.com/search”
response = requests.get(url, headers={‘Accept’:’text/plain’})
print(response.text)
headers={‘Accept’:’text/plain’} This argument returns a plain text version.
JSON (2)
1 stands for JavaScript Object Notation.
2 Python can convert it very quickly to a valid python code.
why do we want json. in dictionary form and how do we do it?
prints the JSON data in the Python dictionary format as seen in the output.
In this way, we can parse JSON responses in Python. and access them via dictionary format.
syntax: x.json()
where x is a variable storing a get request