Skip to content

Force "object"-form when posting json - #40

Merged
stloyd merged 1 commit into
KnpLabs:masterfrom
njam:issue-40
Apr 1, 2013
Merged

stloyd merged 1 commit into
KnpLabs:masterfrom
njam:issue-40

Conversation

@njam

@njam njam commented Mar 31, 2013

Copy link
Copy Markdown
Contributor

When POSTing with no/empty content, we should send {} instead of [], otherwise Github doesn't like it:

Github\Exception\ErrorException (400): Body should be a JSON Hash

stloyd added a commit that referenced this pull request Apr 1, 2013
Force "object"-form when posting json
@stloyd
stloyd merged commit 8eafe2e into KnpLabs:master Apr 1, 2013
@stloyd

stloyd commented Apr 1, 2013

Copy link
Copy Markdown
Contributor

Good catch! Thanks!

@InventisPMTest

Copy link
Copy Markdown

Actually I've noticed that other functions are now entirely broken. An example:

$issue = $client->api('issue')->create(some_account, some_repo, array('title' => 'some_title', 'body' => 'some_body'));
$client->api('issue')->labels()->add(some_account, some_repo, issue['number'], array('bug', 'wontfix'));

This code generates the following error:

The following error occurred: Body should be a JSON Array

Removing the JSON_FORCE_OBJECT parameter fixes the issue. I've worked around this issue by allowing the option to be toggled in HttpClient for now.

It could of course also be that I'm doing something wrong (I'm fairly new to the library), please let me know if I did.

PS: The GitHub API states that the 'body' parameter for an issue is optional, however it is required in the library: http://developer.github.com/v3/issues/#create-an-issue

@njam

njam commented Apr 25, 2013

Copy link
Copy Markdown
Contributor Author

Hm maybe JSON_FORCE_OBJECT should only be passed if $parameters is empty then? I.e. only if we don't send any actual data.
I don't know the Github API good, whether they expect arrays or objects in the nested parameters?

@InventisPMTest

Copy link
Copy Markdown

I also don't know the GitHub API that well (only started with it this week). Modifying line 171 of HttpClient.php to

$request->setContent(json_encode($parameters, empty($parameters)? JSON_FORCE_OBJECT : 0));

... still fixes the issue for me. If this also still fixes the issue (i.e. it doesn't break it all over again) you originally reported, I think this may be a good solution.

What's strange is that the rest of the library has worked mostly perfect up until now (even when I was posting new data on GitHub), it's just setting the labels that's broken.

@njam

njam commented Apr 25, 2013

Copy link
Copy Markdown
Contributor Author

Yes this should be fine - my issue was when the whole body was an empty array/object.
Can you do a pull request with this? (I would personally suggest to not use ternary operator here;)

@InventisPMTest

Copy link
Copy Markdown

Done, it can be found here: http://github.com/KnpLabs/php-github-api/pull/48

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

Successfully merging this pull request may close these issues.

3 participants