[IBM]Python for Data Science, AI & Development - REST APIs & HTTP Reqeusts

2021. 5. 11. 15:44Data science/Python

반응형

HTTP Protocol 

: Uniform Resource Locator (URL)

: a general protocol of transferring information through the web 

: send a request -> recall the REST API's function -> the request is communicated via HTTP message. (contains JSON file) 

URL : the most popular way to find the resources 

1) the Scheme  : the protocol , i.e. http://

2) internet address or Base URL : to find the location i.e. www.ibm.com

3) the route : location on the web server i.e. /images/IDSNlogo.png

 

Request Message & Response Message 

*HTTP method tells the server what action to perform.

** Status code 

*Get to modify the data : you can use GET method to modify the results of your query 

*Create Query string 

url_get ='http://httpbin.org/get'
payload={"name":"Joseph","ID":"123"}
r=requests.get(url_get, params=payload)

r.url:'http://httphin.org/get?name=Joseph&ID=123'
r.requst.body:None
r.status_code:200
r.text {"args":{"ID":"123","name":"Joseph"}, "headers" :{"Accept"...}
r.headers['Content-Type'] 'application/json'
r.json() -> python dict will be returned {"args":{"ID":"123","name":"Joseph"}, "headers" :{"Accept"...}
r.jason()['args'] -> {'ID':'123','name':'Joseph'}

*Post Requests send data to a server, but in a request body, not the URL 

url_post ="http://httpbin.org/post"
payload={"name":"Joseph","ID":"123"}
r_post=requests.post(url_post, data=payload)

Excercise 

import requests 
imports os 
from PIL import Image
from IPython.display import IFrame

url='http://www.ibm.com/'
r=requests.get(url)
r.status_code 
#200

print(r.request.headers)
#result 
{'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 
'Accept': '*/*', 'Connection': 'keep-alive', 'Cookie': 'bm_sz=216F962C5B5BACC68EEEE19
162A77CD2~YAAQpmAZuEfd1T15AQAA7h3fWQtyjq4NclPMUWWQPABsP5eyGR8n86bjNuykogVYVltTVfITVSq
27xOlPbSM7syW/+QdszqIYcBKlIwhGEBQYWuSB4FRjjGCY++vzrBD8jhx5KKb3pakbzm6ozjA/VTLJCH40qZ4U
LGzMvFjz6JNCwNErkOoCyh0uBcs; _abck=91AAD0F30A15BC71D2E544E60F5355CA~-1~YAAQpmAZuEjd1T1
5AQAA7h3fWQU6jls+Lm/XxPE39NKUr9Hr2c6Md2hoeDM0XiR2YdKDSLgkz6TInns9Im0yH1RxnjxrU6iRW9kpA
lhVvAHUt1shCZR/cPZzCWZKs41ZTUf2/7mLHVgqAmSsK55nuyMgthbLJX+fuGCpD5etOGXzdRqo0PWZKOsKCA+7
MXUujs6fuIyhb73NATi/805NuTfNYCedbkQ+POohLz89G/mHAINbssDhUXVZXNV1viuZ2rdaxfnPjPtIVA/xOD
WXmKrutqnfzTYrG3U1A1PGWWnmkevBHH/3k239ph2bYcph36h29aJvaTtZZPJg2TtO5zHdUh/CqG1plq/dMMk
2IiyC4xmAxOI=~-1~-1~-1'}

print("request body:", r.request.body)
#request body: None
#You can view the heade using the attribute headers 
header=r.headers
print(r.headers)

#Obtain the results the request was sent using the key 
header['date']

header['Content-Type']

#Encoding 
r.encoding

#display first 100 characters only
r.text[0:100]

#save the file using a file object(specify the path and name)
path=os.path.join(os.getcwd(),'image.png')
#save it using the open function and write method
with open(path,'wb') as f:
    f.write(r.content)
    
Image.open(path)  

Questions

Write the python code to perform the same task. The code should be the same as the one used to download the image, but the file name should be 'example.txt'.

!wget -O /resources/data/Example1.txt https://cf-courses-data.s3.us.cloud-object-
storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Modu
le%205/data/Example1.txt

#1. url = '' 
url='https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/data/Example1.txt'
#2. Path
path=os.path.join(os.getcwd(),'example1.txt')
#3. get methond
r=requests.get(url)
#4.save the file using open 
with open(path,'wb') as f:
    f.write(r.content)
반응형