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

How to sent empty parameters #973

Closed
rkmax opened this issue Jan 31, 2015 · 9 comments
Closed

How to sent empty parameters #973

rkmax opened this issue Jan 31, 2015 · 9 comments

Comments

@rkmax
Copy link

rkmax commented Jan 31, 2015

I'm having trouble with a site I need pass empty arguments on the query. example:

http://thehost.com?Subject=Hello+World&submit=

i trying to make

$client = new Client();
$response = $client->get("http://thehost.com", ['query' => ['Subject' => 'Hello World', 'submit => null']]);

but guzzle trim the submit parameter, how can solve this issue?

@mtdowling
Copy link
Member

There are two different ways to do this:

  1. Set a query value to an empty string (e.g., '') if you want the trailing "=". (i.e., a=)
  2. Set a query value to null if do not want the trailing "=" (i.e., a).
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client  = new Client();

$request = $client->createRequest('GET', 'http://httpbin.org/get', [
    'query' => [
        'foo' => 'bar',
        'baz' => '', // sends trailing =
        'qux' => null // no trailing =
    ]
]);

echo $request->getUrl();

Outputs: http://httpbin.org/get?foo=bar&baz=&qux

@rkmax
Copy link
Author

rkmax commented Feb 1, 2015

thanks @mtdowling

@dopesong
Copy link

And what about POST request? It should be always with trailing =.

@simPod
Copy link
Contributor

simPod commented Nov 22, 2017

@mtdowling When I set 'qux' => null, the qux param is not added at all. Has something changed during time? :) Thanks!

@AaronJan
Copy link

I had the same issue as @simPod said. Can we add some docs for this behavior?

@elyanory
Copy link

elyanory commented Jul 4, 2018

Same issue... Any idea ? :)

$value = http_build_query($value, null, '&', PHP_QUERY_RFC3986);

@SwordaDev
Copy link

Having the same issue right now

'query' => [
                'input' => $search,
                'location' => $this->eventParameter['location'],
                'radius' => $this->eventParameter['radius'],
                'language' => $this->requestStack->getCurrentRequest()->getLocale(),
                'key' => $this->apiKey,
                'strictbounds' => NULL
            ]

The parameter strictbounds does not affect the result of the request.

@bangize
Copy link

bangize commented Feb 4, 2021

Hi, is there any work around with this issue?

@maiermic
Copy link

It seems, you have to build the query string manually (in guzzlehttp/guzzle since at least version 6.5.5), e.g.

<?php

require_once dirname(__FILE__) . '/../bootstrap.php';

use GuzzleHttp\Client;

$query = join('&', [
  http_build_query(
    [
      'foo' => 'bar',
      'baz' => '', // sends trailing =
    ],
    null, '&', PHP_QUERY_RFC3986
  ),
  'qux', // no trailing =
]);
echo $query . PHP_EOL; // foo=bar&baz=&qux

$client = new Client();
$response = $client->get("https://httpbin.org/get", ['query' => $query]);
echo $response->getBody();

since AFAIK neither Guzzle nor http_build_query support query parameters without value and no trailing =.

@mtdowling

Has something changed during time?

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

No branches or pull requests

9 participants