Tomato
Tomato

Reputation: 779

error: htmlspecialchars() expects parameter 1 to be string when use guzzle


I'm using Guzzle to make a get request and I have a Json response. I tried to pass that Json response to my view but get a error like title.
Here is my controller:

class RestApi extends Controller
{
    public  function request() {
        $endpoint = "http://127.0.0.1:8080/activiti-rest/service/form/form-data?taskId=21159";
        $client = new \GuzzleHttp\Client();
        $taskId = 21159;

        $response = $client->request('GET', $endpoint, ['auth' => ['kermit','kermit']]);

        $statusCode = $response->getStatusCode();
        $content = json_decode($response->getBody(), true);

        $formData = $content['formProperties'];

        return view('formData')->with('formData', $formData);
    }

}

When I use dd(formData), the data is not null:

enter image description here

In my view, I just want to check my formData is passed or not:

@if(isset($formData))
    @foreach($formData as $formDataValue)
    {{ $formDataValue }}
    @endforeach
    @endif

Here is my route:

Route::get('/response','RestApi@request')->middleware('cors');

How can I fix this? Thank you very much!

Upvotes: 0

Views: 502

Answers (2)

Sethu
Sethu

Reputation: 1379

You're trying to display the array {{ $formDataValue }} directly

{{ }} this will support only string

it supposed to be like this for example:

@if(isset($formData))
    <ul>
    @foreach($formData as $formDataValue)
        <li>ID : {{ $formDataValue['id'] }}</li>
        <li>Name : {{ $formDataValue['name'] }}</li>
        <li>Type : {{ $formDataValue['type'] }}</li>
        .
        .
        .
    @endforeach
    </ul>
@endif

Upvotes: 1

Tarasovych
Tarasovych

Reputation: 2398

You have another array here:
enter image description here

So,

@if(isset($formData)) //$formData is an array of arrays
    @foreach($formData as $formDataValue)
        {{ $formDataValue }} //$formDataValue is still an array, so it fails
    @endforeach
@endif

{{ }} accepts only string.

You have to iterate once more:

@foreach($formData as $formDataItem)
    @foreach($formDataItem as $item)
        //here $item would be a string such as "id", "name", "type", but can also be an array ("enumValues")
    @endforeach
@endforeach

Finally

@foreach($formData as $formDataItem)
    @foreach($formDataItem as $item)
        @if(is_array($item))
            @foreach($item as $i) //iterate once more for cases like "enumValues"
                //here you'll have one of "enumValues" array, you have to iterate it again :)
            @endforeach
        @else
            {{ $item }} //you can render a string like "id", "name", "type", ...
        @endif
    @endforeach
@endforeach

Upvotes: 1

Related Questions