How to submit data by post

Time:2020-6-29

When it comes to uploading data, we will naturally sayJson/XML, but these are all data formats. But the way the data is uploaded has always been ignored by me. The current understanding is that four common schemes are commonly used to encode local data throughContent-TypeTo specify the upload method of data, andContent-LengthTo specify the data length.

application/x-www-form-urlencoded

This should be the most common way to submit. In native formFrom, if not setenctypeProperty. This scheme is used by default to upload data. hisHTTPThe request information is as follows:

POST http://www.example.com HTTP/1.1
Content-Type: application/x-www-form-urlencoded;charset=utf-8

titile=%E6%B5%8B%E8%AF%95&body=%E6%B5%8B%E8%AF%95

As you can see,BodyPart of the data goes throughURLAfter transcoding, thekey1=val1&key2=val2In order to arrange. In many cases, this isPostBut because it uses three characters to represent oneNon-ASCIICharacter, so the efficiency is relatively low.

multipart/form-data

Form’senctypeProperty currently has two attribute values to choose from, one of which is the defaultapplication/x-www-form-urlencodedThe other ismultipart/form-dataYes. This method of sending is designed to support sending binary data to the server. hisHTTPThe request information is as follows:

POST http://www.example.com HTTP/1.1
Content-Type:multipart/form-data; boundary=aHR0cDovL3d3dy5leGFtcGxlLmNvbQ

--aHR0cDovL3d3dy5leGFtcGxlLmNvbQ
Content-Disposition: form-data; name="city"

Test
--aHR0cDovL3d3dy5leGFtcGxlLmNvbQ
Content-Disposition: form-data; name="file"; filename="test.png"
Content-Type: image/png
Content-Transfer-Encoding: binary

PNG ... content of test.png ...
--aHR0cDovL3d3dy5leGFtcGxlLmNvbQ--

As you can see from the above example, the format of this sending method includes multiplePart, eachPartRandomly generated--boundaryThe last line uses the--boundary--ending. eachPartMust contain aContent-DispositionField, which contains atypeandnameIf there is a file upload, an additional one is neededfilenameParameters of, wheretypeThe value of isform-data。 Here you can also see thePartIt’s also usedContent-TypeIf it can be the type of binary file to be sent, the corresponding type value will be filled in. If it cannot be identified, it will be used uniformlyapplication/octet-streamAs a type value, if multiple files are aggregated into one form item, themultipart/mixedAs the property value. At the same time, ifPartThe content is the same as the defaultencodingIn different ways, there will beContent-Transfer-EncodingProperty to specify thePartThe coding method of.

application/jsonandtext/xml

These two ways can be seen from the name of the transmission content, not much.

Recommended Today

Django docker containerized deployment: Django docker MySQL nginx gunicorn cloud deployment

In the previous chapter, we added MySQL database to docker, but the development server adopted is easy to use, but it has poor performance and low reliability, so it can not be applied in production environment. Therefore, this chapter will implement the docker + Django + MySQL + nginx + gunicorn container project to complete […]