0

When I am sending a request with Guzzle, it returns a full error message without parsing the JSON inside. Here is how it looks:

""" Client error: POST http://someurl.com resulted in a 409 Conflict response:\n {\r\n "errors": [\r\n "Prospect is already in this campaign"\r\n ]\r\n }\n """

When I am sending the same request via Postman, it does return the response message parsed correctly.

enter image description here

How can I make Guzzle to return only the message - not the full response?

naneri
  • 3,771
  • 2
  • 29
  • 53

2 Answers2

1

It's not clear how you get the string above. Looks like a Guzzle's exception converted to a string. BTW, the string contains the same data as you see in Postman.

Just use $response->getBody()->getContents() as Alive to Die suggested.

Alexey Shokov
  • 4,775
  • 1
  • 21
  • 22
  • I did see that response does contain JSON from Postman. The method that you have posted does not help unfortunately :/ – naneri Aug 23 '17 at 11:56
  • Then the problem is unclear. Please, update your ticket with the code example of other details. – Alexey Shokov Aug 23 '17 at 12:31
1

Encountered the same problem as you. You'd have to setup the Guzzle call this way:

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

The key here is the 'http_errors' => false row. This would remove the error string and simply return the JSON data.

Hope that answers that.

Paolo
  • 390
  • 2
  • 5
  • 20