Laravel route as API callback: How to avoid the "Response content must be a string or object" error?

1.3k Views Asked by At

In my Laravel 5.2 application, I'm using CloudConvert to convert my files. I've implemented asynchronous conversion, which requires a public callback URL to my site. Like this:

public function upload(Request $request) {
    // Store uploaded file...
    CloudConvert::file(/* path to the file */)
        ->callback(action('UploadController@saveFileFromProcess'))
        ->convert('pdf');
}

And the callback:

public function saveFileFromProcess() {
    try {
        CloudConvert::useProcess($request->input('url'))
            ->save(/* path to file storage */);
    } catch (\Exception $e) {
        Log::error($e->getMessage());
        return false;
    }        

    return true;
}

Now, the conversion works just fine. But I can see in the logs that Laravel throws an error after the conversion is done:

The Response content must be a string or object implementing __toString(), "boolean" given.

I understand that this is because a route is called and it returns true or false, instead of e.g. rendering a view.

What should I then return to avoid the error? Is a string enough? Is there anything specific I can return for this kind of call?

And what if I still want to stop the script when e.g. specific Request input is missing?

1

There are 1 best solutions below

1
On

You could return an array with the response, for example return ['status' => true];, which automatically will be converted to JSON and you can use it if you access this route with AJAX.