2

I'm using Guzzle version 6.3 and having issues with my request. I keep getting a 400 Bad Request error and I'm not sure the best approach to debug this or what might be causing the error. When I set this up using Postman and the same criteria all works properly.

$client = new GuzzleHttp\Client();
$body = $this->actionGenerateMessage();

try {
    $response = $client->post('the/endpoint',
        array(
            'body' => $body,
            'headers' => array(
                'apikey' => 'apikeyhere',
             )
        )
    );
} catch (RequestException $e) {
    var_dump($e->getResponse()->getBody()->getContent());
}
user469626
  • 133
  • 2
  • 12
  • Have you verified it works using a more basic means such as `curl` from your shell or even via a REST development app such as Postman? – cnizzardini Aug 19 '18 at 22:20
  • Yes. I've used Postman to test previously and all is working. Now trying to implement into my application and getting the 400 Bad Request response. – user469626 Aug 19 '18 at 22:27
  • Then you have to figure out how to duplicate what you did in postman. Does it use basic auth? Some sort of header digest auth? What are the auth requirements of the API. You need to provide more information if you hope to get help. – cnizzardini Aug 19 '18 at 22:56
  • I am duplicating it from what I can tell. It is using an Apigee end point that connects to Amazon Active MQ. The Apigee end point is set up to only require me to send the API Key as part of the header and then the message. So I'm not sure if it is sending the information correctly and the server is returning the bad request or if my Guzzle setup is not proper for this type of request. – user469626 Aug 19 '18 at 23:06
  • 1
    400 errors happen because there are errors in the syntax of the HTTP request. Does your body match the content type? **edit** ninja'd by OP :) – Loek Aug 20 '18 at 14:14

1 Answers1

2

I was able to get this working. It looks like the ActiveMQ setup was expecting a JSON message when I'm trying to send an XML message.

I was able to determine this by turning on the Guzzle debug flag.

$client = new GuzzleHttp\Client([
    'debug'           => true
]);

Then adding the content-type to the headers.

'headers' => array(
    'apikey' => 'apikeyhere',
    'Content-Type' => 'application/xml',
)

Hope this helps someone in a similar boat.

user469626
  • 133
  • 2
  • 12