0

I am using a Laravel blade.

My code below happens error when the size of item of textarea is huge. I am looking for a way to solve it.

Everyone. How should I send a huge text to server? How should I modify my codes?

Blade(JavaScript)

    function sendByGet()
    {
        var items =  document.getElementById("item").value;
        var param = "?items="+items+"&id={{$id}}";
        var url = {{(url)}} + encodeURI(param);

        let result = fetch(url);
        result.then(response => response.json()).then(
                responceObject =>{
                    
                    }
                } 
    }

Controller PHP

 public function receivebyGet(Request $request)
    {
        $lineArray  = array();
        $slipData = explode("\n", $request->items); 

Error

the date is replaces <huge_text> (Actual data is text(5000 characters))

phpbladeName?id=01:270 GET https://test.com/test/send_by_get?&item=<huge_text> 414 sendByGet @ confirmation?id=01:270 onclick @ confirmation?id=0:244 VM142:1 Uncaught (in promise) SyntaxError: Unexpected end of JSON input at phpbladeName?id=01

user14232549
  • 405
  • 4
  • 17
  • Can you place the error that is happening? Which line of the code, etc. – Marcel Kohls Sep 24 '21 at 06:31
  • If it's because of the URL length you could send the values in the body. At least one browser has a max url length of 2,083 characters. – jabaa Sep 24 '21 at 06:39
  • You can't POST the data? – MahanGM Sep 24 '21 at 06:41
  • 2
    @MahanGM You don't even need a POST. HTTP allows a body in a GET request. It's even common in some widely used tools like Elastic Stack. You should semantically choose the method, not technically. Use a POST if you want to add something to the state. Use PUT if you want to modify an element. Use GET if you don't change the state. – jabaa Sep 24 '21 at 06:43
  • You should not go over 2k URL length. See [What is the maximum length of a URL in different browsers?](https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers) – Peter Krebs Sep 24 '21 at 07:09
  • So you are saying it is impossible to send 2k text data by GET method,right? – user14232549 Sep 24 '21 at 08:51
  • 1
    No, they are saying you have a ~2k size limit for the HTTP query. You should send your data within the body. – Newbie Sep 24 '21 at 08:59
  • 1
    @user14232549 It's not impossible to send more than 2k data using GET but at around the 2k point browser support varies, so with some browsers URLS would work fine up to 8k and with others browsers they will not, leading to some pretty bad user experience. – apokryfos Sep 24 '21 at 09:18
  • @apokryfos , So how would someone send large data, say 5-10k in popular browsers Chrome, Edge, IE, Mozilla? – Ezani Sep 24 '21 at 09:40
  • As others suggested. Use the request body. – apokryfos Sep 24 '21 at 09:59
  • @apokryfos, yes but you just said in your comment above that browser support varies at around 2k size and up to 8k leading to some bad user experience. Is there another way to send these huge data far exceeding the limits you mentioned (2k to 8k) or will the data have to be broken up into manageable sized fragments of around 1k-2k depending on browser? – Ezani Sep 24 '21 at 10:03
  • @jabaa There's also the practical consideration that GET requests are cacheable and the browser will probably not consider the request body when caching the result of a request. – apokryfos Sep 24 '21 at 10:06
  • @Ezani the 2k-8k limit is for the length of the URL. The size of the body generally has no limits in the browser. – apokryfos Sep 24 '21 at 10:09
  • @apokryfos Ok great, thank you very much! – Ezani Sep 24 '21 at 10:09
  • @jabaa The point I was making was that the data is too large to be put in a GET request, so use another method like POST which is made to handle this, or like you said use the body of GET. I wasn't pointing to the semantic side of it. – MahanGM Sep 25 '21 at 09:23
  • @MahanGM The data is not too large to be put in a GET request. The data is too large to be put into the URL. That's completely unrelated to the method. – jabaa Sep 25 '21 at 10:53
  • @jabaa That's what I intended to say, I should have clarified myself so others can understand. URLs support 2048 characters, but most clients and servers work with URLs up to 2000 characters afaik, therefore I said use another method to avoid inserting the data into the URL due to the limitation. – MahanGM Sep 25 '21 at 13:41

1 Answers1

3

Move your data to the BODY

function sendByGet(url) {
    const items =  document.getElementById("item").value;
    const param = encodeURI("?id={{$id}}");

    fetch(url + param, {
        method: 'GET',
        headers: { 'Content-Type': 'plain/text' },
        body: items,
    })
        .then(response => response.json())
        .then( ... );
}

PHP Controller (assuming being Laravel)

public function receivebyGet(Request $request) {
    $lineArray = array();
    $slipData = explode("\n", $request->getContent());
    ...
}

Query size limit

As mentioned by Peter Krebs the maximum URL size (which includes the Query) is 2000 characters. So you cannot expect your system to reliably work if url is longher than 2000. Read more here.

GET body

As pointed out by Jabaa you should semantically choose the method, not technically. But this is something that has evolved over time (initially the GET body was supposed to be rejected/ignored) read more here. Hence you should consider it carefully and verify that all the parties involved (server, browser, proxies, cache) supports it properly. This is why oftentimes developers choose to violate semantics and use a POST method.

Newbie
  • 4,462
  • 11
  • 23
  • 1
    The link is 12 years old. According to my experience times have changed. Widely used software uses GET requests with body. For me that's a strong indication that the problems from 12 years ago are solved today. – jabaa Sep 24 '21 at 10:22
  • Totally agree, I'm not advising against it at all. But it has been standardised in 2014 and nowadays I still encounter systems that are refusing or dropoing the GET body so I feel obligated to warn. – Newbie Sep 24 '21 at 11:04
  • 1
    I didn't want to criticize your answer. I wanted to add my opinion on this point. IMHO a system that drops the body in a GET request is broken. That's a bug in that system. – jabaa Sep 24 '21 at 11:06
  • Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body. at sendByGet (test?id=335) at HTMLInputElement.onclick (test?id=335) – user14232549 Sep 28 '21 at 07:41
  • Yep, as mentioned this is a clear example of unsupported `GET` with body. Sadly you will need to use `POST` instead. – Newbie Sep 28 '21 at 09:28
  • Here a longer discussion https://github.com/whatwg/fetch/issues/551 – Newbie Sep 28 '21 at 09:30
  • If I use POST, csrf error (419 errors)happens in my Laravel project (v.6.2) , I have tried this https://stackoverflow.com/questions/41981922/minimum-working-example-for-ajax-post-in-laravel-5-3 but it does not work on my project. – user14232549 Sep 28 '21 at 10:42
  • Well, you need to obtain the CSRF token somehow. It depends on your architecture. – Newbie Sep 28 '21 at 10:47