smit
smit

Reputation: 1

Fatal error: Uncaught Exception: Error encoding runtime JSON response: Malformed UTF-8 characters in Laravel Vapor

I have setup laravel vapor and deployed my project using laravel vapor. For my web.php file all routes are working and produce HTTP responses, but in api.php when I trigger any API route from a browser it generates an error as below:

Fatal error: Uncaught Exception: Error encoding runtime JSON response: Malformed UTF-8 characters, possibly incorrectly encoded in /var/task/vendor/laravel/vapor-core/src/Runtime/NotifiesLambda.php:49

There is one route which i am calling is : https://sparkling-snowflake-zbnrt7g2gpna.vapor-farm-e1.com/api/ in api.php

Route::get('/', function () {
    return 'This is called from API';
});

But in NotifiesLambda.php it generates error:

protected function lambdaRequest($url, $data)
{
    $json = json_encode($data);//here 

    if ($json === false) {
        throw new Exception('Error encoding runtime JSON response: '.json_last_error_msg()); //here line 49
    }

    $handler = curl_init($url);

    curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handler, CURLOPT_POSTFIELDS, $json);

    curl_setopt($handler, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Content-Length: '.strlen($json),
    ]);

    curl_exec($handler);

    if (curl_error($handler)) {
        $errorMessage = curl_error($handler);

        throw new Exception('Error calling the runtime API: '.$errorMessage);
    }

    curl_setopt($handler, CURLOPT_HEADERFUNCTION, null);
    curl_setopt($handler, CURLOPT_READFUNCTION, null);
    curl_setopt($handler, CURLOPT_WRITEFUNCTION, null);
    curl_setopt($handler, CURLOPT_PROGRESSFUNCTION, null);

    curl_reset($handler);

    curl_close($handler);
}

Any possible solution for this?

I tried

$json = $data;

    // if ($json === false) {
    //     throw new Exception('Error encoding runtime JSON response: '.json_last_error_msg());
    // } commentiong this portion and remove json_encode

but still having error

Upvotes: 0

Views: 31

Answers (0)

Related Questions