How do I resolve a HTTP 414 "Request URI too long" error?

PhpApacheHttp Status-Codes

Php Problem Overview


I have developed a PHP web app. I am giving an option to the user to update multiple issues on one go. In doing so, sometimes the user is encountering this error. Is there any way to increase the lenght of URL in apache?

Php Solutions


Solution 1 - Php

Under Apache, the limit is a configurable value, LimitRequestLine. Change this value to something larger than its default of 8190 if you want to support a longer request URI. The value is in /etc/apache2/apache2.conf. If not, add a new line (LimitRequestLine 10000) under AccessFileName .htaccess.

However, note that if you're actually running into this limit, you are probably abusing GET to begin with. You should use POST to transmit this sort of data -- especially since you even concede that you're using it to update values. If you check the link above, you'll notice that Apache even says "Under normal conditions, the value should not be changed from the default."

Solution 2 - Php

Based on John's answer, I changed the GET request to a POST request. It works, without having to change the server configuration. So I went looking how to implement this. The following pages were helpful:

https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php (Note the sanitize posted data remark) and

http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

Basically, the difference is that the GET request has the url and parameters in one string and then sends null:

http.open("GET", url+"?"+params, true);
http.send(null);

whereas the POST request sends the url and the parameters in separate commands:

http.open("POST", url, true);
http.send(params);

Here is a working example:

ajaxPOST.html:

<html>
<head>
<script type="text/javascript">
	function ajaxPOSTTest() {
		try {
			// Opera 8.0+, Firefox, Safari
			ajaxPOSTTestRequest = new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer Browsers
			try {
				ajaxPOSTTestRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					ajaxPOSTTestRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					// Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}
		}

		ajaxPOSTTestRequest.onreadystatechange = ajaxCalled_POSTTest;
		var url = "ajaxPOST.php";
		var params = "lorem=ipsum&name=binny";
		ajaxPOSTTestRequest.open("POST", url, true);
		ajaxPOSTTestRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxPOSTTestRequest.send(params);
	}

	//Create a function that will receive data sent from the server
	function ajaxCalled_POSTTest() {
		if (ajaxPOSTTestRequest.readyState == 4) {
			document.getElementById("output").innerHTML = ajaxPOSTTestRequest.responseText;
		}
	}
</script>

</head>
<body>
	<button onclick="ajaxPOSTTest()">ajax POST Test</button>
	<div id="output"></div>
</body>
</html>

ajaxPOST.php:

<?php

$lorem=$_POST['lorem'];
print $lorem.'<br>';

?>

I just sent over 12,000 characters without any problems.

Solution 3 - Php

I have a simple workaround.

Suppose your URI has a string stringdata that is too long. You can simply break it into a number of parts depending on the limits of your server. Then submit the first one, in my case to write a file. Then submit the next ones to append to previously added data.

Solution 4 - Php

I got this error after using $.getJSON() from JQuery. I just changed to post:

data = getDataObjectByForm(form);
var jqxhr = $.post(url, data, function(){}, 'json')
    .done(function (response) {
        if (response instanceof Object)
            var json = response;
        else
            var json = $.parseJSON(response);
        // console.log(response);
        // console.log(json);
        jsonToDom(json);
        if (json.reload != undefined && json.reload)
            location.reload();
        $("body").delay(1000).css("cursor", "default");
    })
    .fail(function (jqxhr, textStatus, error) {
        var err = textStatus + ", " + error;
        console.log("Request Failed: " + err);
        alert("Fehler!");
    });

Solution 5 - Php

An excerpt from the RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1:

> The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions:

> - Annotation of existing resources;

  • Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles;

  • Providing a block of data, such as the result of submitting a form, to a data-handling process;

  • Extending a database through an append operation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJProView Question on Stackoverflow
Solution 1 - PhpJohn FeminellaView Answer on Stackoverflow
Solution 2 - PhpatmelinoView Answer on Stackoverflow
Solution 3 - PhpShrey GuptaView Answer on Stackoverflow
Solution 4 - PhpFusca SoftwareView Answer on Stackoverflow
Solution 5 - PhpYour Common SenseView Answer on Stackoverflow