Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Fix issue with JSON payload always having objects instead of arrays
Browse files Browse the repository at this point in the history
Resolves #1
  • Loading branch information
jakezatecky committed Aug 20, 2015
1 parent 927c5eb commit 6e0b217
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/WorkEtcClient/HttpfulClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,36 @@ public function post($endpoint, array $parameters = [])
{
$request = Request::post($endpoint)
->expects('application/json')
->body(json_encode($parameters, JSON_FORCE_OBJECT));
->body($this->jsonEncode($parameters));

return $this->send($request);
}

/**
* Encode the given array to JSON. If the array would not translate to an
* object, force it.
*
* WORK[etc] expects an object, even if that object would be empty.
*
* @param array $array
*
* @return string
*/
protected function jsonEncode(array $array)
{
if (empty($array)) {
return '{}';
}

return json_encode($array);
}

/**
* Send the request and check for errors.
*
* @param \Httpful\Request $request
*
* @return \Httpful\Response
* @return array
*/
protected function send(Request $request)
{
Expand Down

0 comments on commit 6e0b217

Please sign in to comment.