简体   繁体   中英

Why are POST requests often sent with the Content-type set to urlencoded?

If POST data is sent in the body of a request, why use URL encoding?

POST requests require a contentType to be set to describe the data being sent in the body of the request and url encoding is commonly used.

GET requests do not as there is no body. The request parameters are in the URL.

Here is how this looks in code ( works fine ).

/******************************************************************************/
// AJAX

    $P.ajax = function (config_ajax) {
        var xhr;

        // get

        if (config_ajax.type === 'get') {
            xhr = new win.XMLHttpRequest();
            xhr.open('GET', config_ajax.url, true);
            xhr.onload = function () {
                if (this.status === 200) {
                    config_ajax.callback(xhr.responseText);
                }
            };
            xhr.send(null);
        }

        // post

        if (config_ajax.type === 'post') {
            xhr = new win.XMLHttpRequest();
            xhr.open("POST", config_ajax.url, true);
            xhr.setRequestHeader("Content-type",
                    "application/x-www-form-urlencoded");
            xhr.onload = function () {
                if (this.status === 200) {
                    config_ajax.callback(xhr.responseText);
                }
            };
            xhr.send(config_ajax.data);
        }

Here you can see the send data for GET is null and the send data for POST is populated and also that the POST data is url encoded () and the GET data is not.

url encoding is not default as if you do not specify it or some other encoding, an error will be thrown.

I guess, what I'm asking, is why can't I leave it off ( the contentType for POST ) and have the data transfer?

Or if by the specification a Content-type is required. Is there something I can use better than URL encoding.

As stated above, because the data is not in the URL and does not need URL encoding, I would prefer to use something more simple.

My guess is that this is not possible, that this is just a default, that has been used before Ajax, perhaps when POST requests did use the URL?

If POST data is sent in the body of a request, why use URL encoding?

You have to use some sort of encoding.

Most POST requests coming from a browser are submitting form data.

Form data sent in a GET request uses URL encoding.

Using the same encoding means that you can use the same serializer and parser.

I guess, what I'm asking, is why can't I leave it off ( the contentType for POST ) and have the data transfer?

Because you'd be sending some data and not telling the recipient how it should be decoded.

Or if by the specification a Content-type is required. Is there something I can use better than URL encoding.

You can use any encoding you like. URL encoding is convenient and works for most data.

I would prefer to use something more simple

URL Encoding is very simple, and JavaScript makes it easy to generate thanks to encodeURIComponent .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM