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

6

124

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?

Hyperbaric answered 23/5, 2010 at 11:50 Comment(1)
If you're seeing this error on a Windows server and/or in an IIS / ASP.NET app, see question: https://mcmap.net/q/130940/-http-error-414-the-request-url-is-too-long-asp-net/12484Crystalcrystalline
B
186

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."

Beal answered 23/5, 2010 at 11:55 Comment(9)
I tried using POST at first, but this is an update operation on the database, and I am refreshing the orginal page using the values that were originally posted to that page.Hyperbaric
JPro: Updating a database is more or less the exact reason you would use POST. Nothing about using POST precludes you from populating the same form with the fields that were just posted, so I'm not sure what you mean by that.Beal
@JPro: The usual technique in that case is to POST to the same page. The handler for the page (which can be the same code for both GET and POST) first checks for POST parameters, handles them if it finds them, then returns the page with the proper values filled in, which will be either the updated values (if POST and the update succeeds) or the original values (if GET, or if POST and the update fails). If the update fails, you can even have per-field error messages describing the failure.Manthei
I figured this out pretty late, so I would like to share it. If you can't find the word LimitRequestLine anywhere in your httpd.conf file, just add the line yourself anywhere you like. For example: LimitRequestLine 100000Ballottement
Hi @MikeDeSimone, I added 1 line "LimitRequestLine 100000" to httpd.conf but it can not fixed, is there any way to resolve it? Could you please help me? Thank you!Fortuna
"it can not fixed" tells me nothing. Also, I'm not sure Apache will allow 100k request lines; that's a really over-the-top size. You probably need to rethink your approach, and use something else besides GET if you have that many query parameters.Manthei
@JulesColle any idea if that didn't help what I could do? I set it to 20000 and my URL is just ~17500 so it should work . I heared it has maximum that you can set so that's the reason I set it bit smaller than you suggested.Schargel
Thank you, for XAMPP on Ubuntu 18.04 I had to add LimitRequestLine 20000 head of /opt/lampp/etc/httpd.conf file.Likker
I tried to add new line LimitRequestLine 20000 to /etc/apache2/apache2.conf and restart apache but still error. please helpIscariot
A
18

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:

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.

Amok answered 30/4, 2014 at 3:30 Comment(0)
P
5

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.

Parabola answered 4/9, 2012 at 14:36 Comment(4)
Can you provide an example? I can see how you split the string when it's user generated...Smarm
Very cheap workaround. It is better to reconsider the domain problem!Goblet
This doesn't deserve to have so many downvotes. There certainly are situations in which submitting multiple requests can be an acceptable workaround. True, the quality of the answer is a bit low, but that's to be expected from a user who is brand new to SO. Let's show some love and offer feedback instead of just downvoting newcomers that don't "get" SO yet!Bachman
I agree, it looks viableBelshin
W
3

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!");
    });
Welch answered 16/11, 2016 at 11:20 Comment(2)
is this an answer or question?Projector
This is a good quick fix. Changing from get to post allows the long URL without any server config change.Burkhardt
R
1

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.
Romanist answered 23/5, 2010 at 12:6 Comment(2)
Not seeing how this answers the question..?Stephanotis
The original poster said that the records are being updated. For updates, it is good practice to use POST or PUT and not GET. But, of course, it might be that the URL max limit gets exceeded when retrieving the records to display before updating, and then GET method is appropriate, but it can fail because of this limit. The original poster didn't mention at which stage the problem arises, so it can be assumed that it was during the update itself, but we can't be sure...Unhinge
C
0

I also got this error when I was testing woocommerce rest api with Postman.

and the problem was because the authorization data were sent in the request body. When I sent them in the header, the problem was solved.enter image description here

Clute answered 28/2, 2023 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.