transmitting requests --- requests data type json

  We all know that post request body There are four data types, today we have to write an article on requests json request this data type.

 

type of data

We all know that post and get big difference is that there is a body and get no post presence body, then the common four kinds of body types of data you know?

application/json:

DETAILED expressed json request data format, if not set enctype attribute, then it will in the final application / x-www-form-urlencoded submission data (POST enctype the default)

format

{"name":"value","name1":"value2"}

application/x-www-form-urlencoded

Browser native form form the default data format

format

name=value&name2=value2=name3=value3

We use the form to upload files, you can also upload common data, equal to just let the form of enctype multipart / form-data on it

------WebKitFormBoundaryBRi81vNtMyBL97Rb
Content-Disposition: form-data; name="name"

name1
------WebKitFormBoundaryBRi81vNtMyBL97Rb
Content-Disposition: form-data; name="age"

12
------WebKitFormBoundaryBRi81vNtMyBL97Rb--

text/xml

For parameter passing in the form of xml

<!--?xml version="1.0"?-->

<methodcall>

<methodname>examples.getStateName</methodname>

<params>

<param>

<value><i4>41</i4></value>

</params>

</methodcall>

 

Send requests json

1, import requests library

2, find the requested address, fill in the body

Import Requests 
URL = " http://httpbin.org/post " 
# Add json data 
json = {
     " username " : " AnJing " ,
     " password " : " 123456 " 
} 
# Add json data by way of 
r = requests.post ( URL, JSON = JSON)
 Print (r.text)

3, sends a request to view the content returned

By content, we can see the return of the data type of the parameter to json

 

 Attentive little friends can request to return json find ways and means of data requests is the same, different parameters is not the same as the requested type.

 

 

Through a simple interface, we learned how to send json request, small partners to go hands-on try it.

 

 

Guess you like

Origin www.cnblogs.com/qican/p/11378889.html