Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
RootProgger opened this issue Dec 15, 2016 · 7 comments
Closed

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

RootProgger opened this issue Dec 15, 2016 · 7 comments
Assignees
Labels

Comments

@RootProgger
Copy link

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
Copy link
Member

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
Copy link
Author

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

@florianpreusner
Copy link
Member

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
Copy link
Author

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

@florianpreusner
Copy link
Member

florianpreusner commented Dec 15, 2016

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
Copy link
Member

florianpreusner commented Dec 18, 2016

I created a new ticket regarding the override of content-type to split these two "bugs": #84

florianpreusner pushed a commit that referenced this issue Dec 18, 2016
Fix: Content-Type in config.yml doesn't work #82
@florianpreusner florianpreusner changed the title Content-Type in config.yml doesn't work Header Content-Type in options doesn't work Dec 18, 2016
@florianpreusner
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants