A first request interface Python

1. Ordinary get request

Requests Import 
Import JSON 
login_res = requests.post (URL = 'HTTP: //joy.web.com: 8090 / Login', Data = { "username": "Joy", "password": "joypawd"}) 

# Returns the text string type 
Print (login_res.text) 

# returns json format of the content type of dictionary (null return None) 
Print (login_res.json ()) 

# returns information cookies 
Print (login_res.cookies) 

# returns the byte stream (cracks ) 
Print (login_res.content.decode ( 'UTF8')) 

# returns the byte stream 
Print (login_res.content) 

# returns the response headers 
Print (login_res.headers) 

# return a status code 
print (login_res.status_code)

2.json format request

Requests Import 
Import json 


url = "https://cp.joy.com.cn/api/get" 
values = { 'Id': 'n6085MMV', 'userId': '89', 'LOCATION': 'Shenzhen' } 
# print data type values, the output of <class 'dict'> 
Print (type (values)) 
Print (values) 
# the json.dump objects coded into the python json string 
values_json = json.dumps (values) 
# printing encoded into values_json the json string data type, output <class 'STR'> 
Print (type (values_json)) 
Print (values_json) 
# requests submitted data library post request 
req = requests.post (url, data = values_json, headers = { 'file application / json'}): 'the type-the Content' 
# json print data Unicode encoding format 
print (req.text) 
requires a corresponding type of object is serializable json # when using json.dumps () 
Change = REQ.json() 
# json.Use the default ASCII encoding of Chinese dumps when serialization, if without any configuration of the print are ascii characters, you need to specify the output of Chinese ensure_ascii = False
= json.dumps NEW_REQ (Change, ensure_ascii = False) 
# print data interface has returned, and the Chinese encoding 
print (new_req)

  

 

 

Guess you like

Origin www.cnblogs.com/joy-sir/p/12187242.html