Recently an asset management system project, one of which is the client sends the collected data to the service side, the server is Django write, the client needs to use the Rrequests module to simulate sending requests
Suppose that the data sent is this:
data = {' status ': True, ' content ': {' K1 ': ' xxxx ', ' K2 ': ' XXXX '}}
is a dictionary set dictionary, and then sent through requests.post (url = URL, data = data), you will find the data received on the server is such, <querydict>:{[' status ', ' content '}
What happened, why only the key, but did not take the values, since you can take the key, it means that Django is no problem
The question must have been on requests.post sending the data.
All right, the cushion is over.
Imagine how Ajax sends data.
$.ajax ({
URL: ' xxxx ',
Data: {
K1:1,
K2: ' abc ',
K3: [1,2,3,4, ' s ']
}
})
Now, you can only send these three formats, no dictionary, if you want to send a dictionary, you can convert the dictionary into a string format via JSON, Json.stringfy ({' K1 ': ' xxx '})
The form form is the same, and Requests.post is a fake form form submission
Discover the problem, let's dissect the process of sending a POST request to HTTP
The data sent by Ajax is a dictionary, which is the form of a key-value pair, which converts this key-value pair into an HTTP post-request process.
K1=XXX&K2=XXX This format and brings up a request header:
content-type:application/x-www-form-urlencoded
This request header and k1=xxx&k2=xxx This format is corresponding to one by one, as long as the format is sent, you must bring this request header, the background of request. The post will parse the format from this request header and revert to the format of the previous dictionary. And this format and this request header is the default, so if we do not modify, directly send the article began to say that format, the server can not parse, so only to get the dictionary key, did not get the value
But if this is the case, we are not sending this format, but want to send json.stringfy ({' K1 ': ' xxx ') This JSON string, there is a way, Ajax can customize the request header, change to this:
$.ajax ({
URL: ' xxxx ',
Headers: {' content-type ': ' Application/json '},
Data:JSON.stringfy ({k1:1,k2: ' abc ', K3: [1,2,3,4, ' s ']})
})
This allows the backend to parse the received data (a JSON-formatted string) based on the JSON
But you can't use request. Post, because request. The POST defaults to parsing the data in the format k1=xxx&k2=xxx,
You should use Request.body to get the data, and then use Json.loads to get the dictionary.
By default, form forms, Ajax, and Requests.post are all handled this way.
So in this project, we need to customize this request header with requests, and this is a simple notation.
Requests.post (url = URL, json = data), it is OK to replace the original data keyword parameter with the JSON keyword parameter.
What's inside is, 1. Serialization of data,2. With a request header ' Content-type ': ' Application/json '
In this way, the server to get the data, first decode decoding, in Json.loads, is our familiar dictionary
About form forms or Ajax sending data to the background, the data format is explored