8p / EightPointsGuzzleBundle

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

Header Content-Type in options doesn't work #82

Closed RootProgger closed 7 years ago

RootProgger commented 7 years ago

i try to setup a handler in config.yml, but the "Content-Type" doesn't work in request. config.yml:

guzzle:
  clients:
    test:
      base_url: "testurl"
      options:
        auth:
          - "username"
          - "passwort"
        verify: false
        debug: true
        headers:
          Content-Type: "application/x-www-form-urlencoded; charset=utf-8"

Now when i call:

$client = $this->get('guzzle.client.test');
$request = $client->post(url, ['form_params'=> $body]);

the charset is not set. But when i set the headers in post-call:

$request = $client->post(url, ['form_params'=> $body, 'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'
            ]]);

The Charset is correct set

florianpreusner commented 7 years ago

You are totally right. I will fix that asap. Until than you can use a workaround to set headers by the deprecated approach (using headers directly on root hierarchy of client.

Example:

guzzle:
  clients:
    test:
      base_url: "testurl"
      ## START: DEPRECATED BUT WORKING APPROACH
      headers:
          Content-Type: "application/x-www-form-urlencoded; charset=utf-8"
      ## END: DEPRECATED BUT WORKING APPROACH
      options:
        auth:
          - "username"
          - "passwort"
        verify: false
        debug: true
RootProgger commented 7 years ago

I try that too but that also doesn't work. Only when i set the Content-Type in Post-Call it work.

florianpreusner commented 7 years ago

I've tried it again and it works for me by using following code: config.yml:

guzzle:
    logging: true
    clients:
        httpbin:
            base_url: "https://httpbin.org"
            headers:
                Content-Type: "application/x-www-form-urlencoded; charset=utf-8"
            options:
                verify: false
                debug: true

Controller:

    public function indexAction(Request $request)
    {
        $client = $this->get('guzzle.client.httpbin');
        $params = ['form_params'=> ['test' => 'aha']];

        $response = $client->post('/post', $params);
        $body = $response->getBody()->getContents();

        var_dump($body);

Output Request:

bildschirmfoto 2016-12-15 um 10 22 38

I used dev-master which is similar to latest stable version 5.2.1.

RootProgger commented 7 years ago

Look at your Content-Type in Request, there's no charset only type

florianpreusner commented 7 years ago

You are right. Didn't saw that, sorry. Unfortunately this is something that comes from Guzzle Client. By using form_params this header will be overridden by Guzzle: https://github.com/guzzle/guzzle/blob/master/src/Client.php#L303

So without using form_params everything works like expected (except using headers in options, which is still a bug).

florianpreusner commented 7 years ago

I created a new ticket regarding the override of content-type to split these two "bugs": https://github.com/8p/GuzzleBundle/issues/84

florianpreusner commented 7 years ago

Released v5.2.2, fix: headers in options https://github.com/8p/GuzzleBundle/releases/tag/v5.2.2

Issue with form_params and content-type will be continued in #84