8p / EightPointsGuzzleBundle

⛽️ Integrates Guzzle 6.x, a PHP HTTP Client, into Symfony
MIT License
440 stars 71 forks source link

Client error: `400 Bad Request` response ??? #204

Closed suiXz closed 6 years ago

suiXz commented 6 years ago
Q A
Symfony version 3.4.11
Bundle version 7.3.1

Hi guys, first time for me here !

I've used the bundle like this :

# config.yml
eight_points_guzzle:
    clients:
        api_client:
            base_url: "%url%"
            options:
                auth:
                    - "login" # login
                    - "pass" # password
                headers:
                    content-type: "application/xml"
                    Accept: "application/xml"

Injected my client into a service class :

# Service Class .php
    public function __construct(
        ContainerInterface $container,
        LoggerInterface $logger,
        Client $client
    )
    {
        $this->container = $container;
        $this->logger = $logger;
        $this->client = $client;
        $this->apiUrl = $this->container->getParameter('url'); //
    }

And used him like that :

# Service Class .php method
$response = $this->client->post(
                static::SERVICE_URI . $uriAction, // This is a const for user path '/customers/v1/' and $uriAction is 'createCustomer' so (customers/v1/createCustomer)
                [
                    'body' => $options['body'], // in the body its a full xml string
                    'content-type' => 'application/xml'
                ]
            );

And i get this exception :

Client error: `POST https://url/blabla/customers/v1/createCustomer` resulted in a `400 Bad Request` response:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns43:errors xmlns="http://url/blabla"  (truncated...)
gregurco commented 6 years ago

Hello @suiXz . If I remember correctly, it's normal behavior for guzzle. If you don't want to receive exception in case of non 2xx response then setup http_errors option to true:

eight_points_guzzle:
    clients:
        api_client:
            base_url: "%url%"
            options:
                http_errors: false
                auth:
                    - "login" # login
                    - "pass" # password
                headers:
                    content-type: "application/xml"
                    Accept: "application/xml"

Read more about it here: http://docs.guzzlephp.org/en/latest/request-options.html#http-errors

And please write if it's the fix of your problem :slightly_smiling_face:

suiXz commented 6 years ago

Yeah it works thanks !

gregurco commented 6 years ago

@suiXz you're welcome :slightly_smiling_face: