1

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?

monday
  • 287
  • 2
  • 3
lesssugar
  • 15,486
  • 18
  • 65
  • 115

1 Answers1

3

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.

thefallen
  • 9,496
  • 2
  • 34
  • 49
  • Yep, I also think that's the way to go. It's an API call, so returning JSON is legit. I ended up doing `return response()->json(['success' => false], 500)` for errors and `return response()->json(array('success' => true), 200)` if all is fine. Thanks. – lesssugar Jun 16 '16 at 09:16