In the interface test, the typesetting format of the request body has JSON format and form format. These two formats are not data types of a certain language, but can be understood as a typesetting format.
JSON format
JSON is a grammar for storing and exchanging textual information, similar to XML, but smaller, faster and easier to parse than XML
Two structures of JSON:
- Object : Represented by curly braces {}, the object is composed of attributes, and the attributes are composed of key-value pairs (key:value), and multiple attributes are separated by commas. The key must be enclosed in double quotes . Such as: {"a":1,"b":2}
- Array : Represented by square brackets [], the array is composed of individual values. Such as: [12,'abc',"Hello",false,null,]
There are four basic types of JSON: string, number, boolean, and null
. When the value is a string, it must be enclosed in double quotation marks. true/false/null must be lowercase
The value of JSON nested JSON object or JSON array can be another object or array, such as:
- Object set array: {"Name":"Marry","Transcript":[60,70]}
- Set of objects: {"Name": "Marry", "Transcript": {"Chinese": 60, "Math": 70}}
- Array set of objects: [60,70,{"English":80}]
- Array set of array: [60,70,["haha","Marry"]]
JSON and python dictionary distinction
-
JSON keys can only use double quotes, python keys can use single quotes, double quotes, and triple quotes
-
The value type of JSON can only be: string, number, boolean, empty value, object, array;
the value type of python can be: string, number, boolean, list, tuple, dictionary, empty value -
In python, the JSON format string can be converted into a dictionary through json.loads(str)
-
In python, a dictionary in JSON format can be converted to a string through json.dumps(str)
Form format
The form format is also called form format, or x-www-form-urlencodeed format. It is composed of key-value pairs (key=value), multiple key-value pairs are connected by &, and the key-value pairs do not need quotation marks. For
example: name=Marry&Score=60
When the two formats are mixed together, such as name=Marry&Score={"Chinese":60,"Mathematics":70}, it is still a form format
Different data formats are used in python requests
When using requests to send a request in python, you need to specify the data format when there is a request body. Generally there are data, json, files, params.
- data : The request body is the use of the form format, that is, when the Content-Type: application/x-www-form-urlencoded in the request header, use data to pass the request body
such as: requests.post(url,data={"name": "Marry","pwd":123456}) - json : The request body is the use of json format, that is, when the Content-Type: application/json in the request header, json is used to transmit the request body. For
example: requests.post(url,json={name=Marry&pwd=123456}) - files : Use of file upload interface
- params : used by general get requests