1

I am developing a web app that connects to Xero's API to fetch Contacts and Invoices using Laravel Xero. At first it was fetching the data properly. Then the cURL 18 error started to appear erratically. And now the error has become permanent.

Checking Xero's Developer dashboard, the calls I make apparently get a status 200 which makes me believe that the error is truly from my end.

Here's the code when making the call:

protected function guzzle ($type, $request, $data = [], $raw = false)

   {
        try {
            
            $client = new Client;

            $headers = [
                'Accept'         => 'application/json',
                'Authorization'  => 'Bearer '.$this->getAccessToken(),
                'Xero-tenant-id' => $this->getTenantId(),
                'Accept-Encoding' => 'gzip, deflate',
            ];

            $response = $client->$type(self::$baseUrl.$request, [
                'headers' => $headers,
                'body'    => $raw ? $data : json_encode($data),
            ]);
            

            return [
                'body'    => json_decode($response->getBody()->getContents(), true),
                'headers' => $response->getHeaders()
            ];
            
            
        } catch (ClientException $e) {

            throw new Exception($e->getResponse()->getBody()->getContents());

        } catch (Exception $e) {
                  
            throw new Exception($e->getMessage());
        }
    }




The cURL 18 error starts to appear upon $response, with the exception being caught at the catch (Exception $e) function.

I have tried virtually every suggestion found on the web and haven't had any success.

Thank you in advance for any help.

For Reference: cURL error 18: Transfer closed with outstanding read data remaining

fufubrocat
  • 204
  • 1
  • 7
  • 1
    Which part of curl error 18 _"Transfer closed with outstanding read data remaining"_ is unclear to you in context of your issue? What are you wondering specifically about? – hakre Jul 20 '23 at 06:36
  • 1
    Hello @hakre, quite frankly I do not know how to deal with the error anymore. – fufubrocat Jul 20 '23 at 06:45
  • 1
    Yes, sure, otherwise you would not ask. And thinking asking myself: what is _your_ understanding of the error message? This is not a trick-question or similar by me, just to help you getting out of "everything does not work, error error error" situation. Now you've found an error, be proud ;) Everything you can't break does not exists. Jokes aside, I'm really interested what your thoughts are, just about that message. – hakre Jul 20 '23 at 06:51
  • 1
    My understanding is that for some reason, at the process of pending data still being fetched from Xero, something is causing the cessation of the data transfer. Does that help? – fufubrocat Jul 20 '23 at 07:03
  • 1
    Yes, sure, that's your understanding. Thanks! And looking into existing Q&A for that error message, it seems there are a couple of pointers already: https://stackoverflow.com/q/1759956/367456 - Perhaps one of those rings a bell in your context? And a hint: when you re-throw exceptions, make use of the $previous constructor parameter and put the previous exception in there. This helps to not loose important information (and perhaps never throw Exception and instead think about a more fitting one). – hakre Jul 20 '23 at 07:04
  • No findings yet? If I get the error classes right, it is either that the data is cut-off unexpectedly or that the provided content-length number is higher than the actual count of bytes. Both signals a problem with the endpoint you send the request to, not with your code. Try with curl on the commandline and verbose to reproduce, as it is easier to review the response headers. Another approach is to work with wireshark. But I'm personally trying to reproduce with curl on the command line first as it is self-documenting. – hakre Jul 20 '23 at 10:11
  • Hello @hakre. Thanks for the suggestions and apologies for the delayed response. Still no luck and am beginning to think that there's a really a problem with the endpoint. – fufubrocat Jul 24 '23 at 03:23
  • Latest thing that I noticed is that if I keep refreshing the page, it sometimes works. But most of the time it doesn't. – fufubrocat Jul 24 '23 at 03:55

1 Answers1

0

I've recently been running into the same issue of getting intermittent cURL error 18 errors despite Xero reporting that the call went through successfully on their end. Been driving me nuts, but finally found a fix.

Try setting the following in the header of your affected GET requests:

'Content-Length:'

So not specifying any value for the Content-Length at all, but still including it in the header. Telling Xero that my GET calls do not have any body in this manner has resolved the issue for me.

DashRantic
  • 1,448
  • 4
  • 19
  • 32
  • 1
    Thanks @DashRantic. While the 'Content-Length:' suggestion didn't help with my issue right away, it was actually your "Telling Xero that my GET calls do not have any body in this manner has resolved the issue for me." feedback that gave me the idea to remove 'body' from the header. So far my calls are being returned properly. Will mark your answer as accepted after observing consistency for a bit more. Thank you so much! – fufubrocat Jul 31 '23 at 03:10