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

resulted in a 400 Bad Request response: #222

Closed
bobykurniawan11 opened this issue Jul 14, 2018 · 5 comments
Closed

resulted in a 400 Bad Request response: #222

bobykurniawan11 opened this issue Jul 14, 2018 · 5 comments

Comments

@bobykurniawan11
Copy link

bobykurniawan11 commented Jul 14, 2018

Hi, i'm trying to create new record. here is what i do.

function createData($params){
$randomkey = $this->firebase->getDatabase()->getReference($this->reference)->push()->getKey();
		$this->firebase
				->getDatabase()->getReference($this->reference)
				->getChild($randomkey )->set($params);
	}

and $params

$data = [
    'fullname' => 'My awesome post title'
];

but i get this error message

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://blablaxxxi.firebaseio.com/posts resulted in a 400 Bad Request response:
{
"error" : "No data supplied."
}

in D:\xampp\htdocs\firebase-php\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113
Stack trace:
#0 D:\xampp\htdocs\firebase-php\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
#1 D:\xampp\htdocs\firebase-php\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response))
#2 D:\xampp\htdocs\firebase-php\vendor\guzzlehttp\promises\src\Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)
#3 D:\xampp\htdocs\firebase-php\vendor\guzzlehttp\promises\src\TaskQueue.php(47): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise{closure}()
#4 D:\xampp\htdocs\firebase-php\vendor\guzzlehttp\promises\src\Promi in D:\xampp\htdocs\firebase-php\vendor\kreait\firebase-php\src\Firebase\Exception\ApiException.php on line 40

everything else are working (Read, update and delete)

After another try and I'm totally sure the error happened when generating the $randomkey. Because when i do this

$this->firebase
				->getDatabase()->getReference($this->reference)
				->getChild("asdfasdf")->set($params);

it is working .

sorry for my english

@jeromegamez
Copy link
Member

Too bad you deleted your second comment and changed the original issue description, but I’m glad you got it working!

@simone-b
Copy link

Hi !

@bobykurniawan11 I fixed the issue by using it this way (passing an empty array to push method) :

$firebase->getDatabase()->getReference($this->reference)->push([])->getKey();

@jeromegamez : The problem can be fixed by setting default value of $value at an empty array instead of a null value in Kreait\Firebase\Database\Reference@push line 351.
Because then when Guzzle is called with a null value for key json, guzzle doesn't add a body in the request. And firebase APIs needs at least an empty json :

{

}

Hope that it'll help you !

@jeromegamez
Copy link
Member

Thanks for looking further into it and the hint, @simone-b! ->set() has a similar issue. To re-iterate:

  • It should be possible to push to a reference without a value just to get a key
  • When setting a reference with null, the reference is deleted.

I'll work this in and create a new release as soon as I'm done!

@jeromegamez
Copy link
Member

@bobykurniawan11 @simone-b Here it is: https://github.com/kreait/firebase-php/releases/tag/4.13.1

@lock
Copy link

lock bot commented Oct 23, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants