python- Interface - Basics (2: Call)

1.

http

1)

get

import request

requests.get(url,params=params,**kwargs)

requests is carrying too python interface library, the interface may transmit the reference value information source

post

import request

requests.post(url,data=data,json=json,**kwargs)

requests is carrying too python interface library, the interface may transmit the reference value information source

2) When we do the project in general will read the data from the configuration file / excel, or database, which involved the request header has a fixed type restrictions (refer to interface documentation, or view the Content-Type capture relevant information here, but more were introduction) This requires us to read the data type of understanding, which does not comply with interfaces for data transfer ahead of time to convert the current to introduce the json library dumps () dict conversion method, loads () method convert str as follows:

header_dict = { "Content": " appliction / json", "ll": " Character not easy to point at least Baidu"}
header_str = '{ "the Content": "Appliction / JSON", "LL": "Character useful point Baidu without at least '}'
header_str_1 = "{ 'the Content': 'Appliction / JSON', 'LL': 'Character least not easy point Baidu'}"

# loads, str turn dict
#. 1) regular type str
A = json.loads (header_str)
Print (A)
Print (type (A))
# 2) irregular type str, str normal rotation dict
a_1 = json.loads (header_str, encoding = 'UTF-. 8')
Print (A_1)
Print (type (A_1))
# dumps, dict turn STR
#. 1) the content of Chinese characters, the Chinese character is not converted, content encoding problem
b = json.dumps (header_dict)
Print (b)
Print (of the type (b))
# 2) to solve the problem of Chinese characters can not be transferred
B_1 = json.dumps (header_dict, ensure_ascii = False)
Print (B_1)
Print (of the type (B_1))

Guess you like

Origin www.cnblogs.com/newsss/p/12108344.html