Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass errors from API Respose to Laravel view using Guzzle

Tags:

laravel

guzzle

Here's my code:

public function store(Request $request)
{
    try {

        $request->merge(['subject' => 'Subject']);

        $client = new Client(['base_uri' => 'http://crm.vulcan/api/v1/']);

        $res = $client->request('POST','contactForm',[
            'headers' => [
                'Accept' => 'application/json'
            ]
        ]);

    } catch (RequestException $e) {
        return Redirect::route('contact.form')->withErrors($e->getResponse());
    }
}

It doesn't work because I can't figure out how the $e->getResponse() is supposed to work.

If I do dd($e->getResponse()) then I get this:

Response {#330 ▼
  -reasonPhrase: "Unprocessable Entity"
  -statusCode: 422
  -headers: array:7 [▶]
  -headerLines: array:7 [▶]
  -protocol: "1.1"
  -stream: Stream {#328 ▶}
}

The reasonPhrase and statusCode are exactly what I was expecting, so no problems there.

However, all I actually want is the JSON object from the API which says which fields didn't validate. I know the object is there because I can see it when I sent a POST through Postman. And, bizarrely enough, if I do a return on the exact same $e->getResponse then I can see the object, too:

{
"name": [
    "The name field is required."
],
"nickname": [
    "The nickname field is required."
],
"email": [
    "The email field is required."
],
"subject": [
    "A subject must be provided"
],
"body-text": [
    "The body-text field is required."
]
}

That is exactly what I need to pass to withErrors() and then I'll be done, but I just can't figure out how to do this.

I have a feeling that I'm misunderstanding something regarding streams, but I've read about PSR7 and streams and I'm afraid I do not understand what any of it means or how it's relevant to this particular issue.

EDIT

After a bit more fiddling about, I updated the catch to the following:

        $errors = json_decode($e->getResponse()->getBody()->getContents());

        return Redirect::route('contact.form')->withErrors($errors);

That appears to work as I get the JSON object of the errors in a format Laravel can use for the form.

like image 287
Joseph Avatar asked Jan 01 '26 05:01

Joseph


1 Answers

This should work to handle the HTTP code without throwing an exception:

public function store(Request $request)
{
    $request->merge(['subject' => 'Subject']);

    $client = new Guzzle([
        'base_uri' => 'http://crm.vulcan/api/v1/'
    ]);

    $res = $client->request('POST','contactForm',[
        'http_errors'=>false,
        'headers' => [
            'Accept' => 'application/json'
        ]
    ]);

    if ($res->getStatusCode() == 422) {
        //then there should be some validation JSON here;
        $errors = json_decode($res->getBody()->getContents());
    } 

    return Redirect::route('contact.form')->withErrors($errors);
}
like image 200
Mike Miller Avatar answered Jan 04 '26 11:01

Mike Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!