Python 1. 2. a) Suppose you are working on a program that requires an API call t
ID: 3900080 • Letter: P
Question
Python
1.
2.
a) Suppose you are working on a program that requires an API call to some website called http://ww.booisawesome.com. We wish to access the customers API webpage of the website-the API takes the following two parameters: date 12-5-16 search dog Write the encoded URL that we would use in order to send a HTTPS request to the web API for the BoolsAwesome website in the box below. b) When programming a function to generate the encoded URL as you wrote in part (a), why do we use the function urlib.parse.urlencode instead of just concatenating a string for the URL ourselves? (c) After getting the result of the HTTPS request from part (a), we parse the JSON response. Why do we use the json library to parse the JSON response? In other words, how did the library make it easier for a program to process the information in the JSON response? (d) What is the purpose of using protocols in writing programs?Explanation / Answer
Answering your first question completely:
a)
The URL to access the website will be same as https://www.booisawesome.com, however, the query paramteres will be appended to the URL. So complete URL will be:
https://www.booisawesome.com/?date=12-5-16&search=dog
b)
The reason for using urlencode is sometimes, we have special characters in the query paramteres such has '/' or '%' which have special meaning in URL request. '/' is used to define the resource path at server. So we need to convert those characters to something which server can convert at its end, so URLEncode converts those special characters to its ASCII equivluents and then send across.
c)
The response returned from the server is always a kind of plaintext string. Now if server wants to send a student object, where the object has keys as name and age, then server will convert student object to JSON object like below:
{"name": "xyz","age": 18}
Now this plaintext string is sent to the clien as server response. Now in order to interpret the values in the response, server again need to convert this plain text string to the object notation. The JSON library will parse the string response to the Specific object and then we can proces this.
d)
The protocols such as http and https are used to specify how should the information travel across incternet. In HTTP, the information can be decrypted by hackers in middle, but with HTTPS, we use a SSL certificate, due to which all the information travells in network in encrypted format. However, significant costs are incurred when we buy a SSL certificate for https.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.