Skip to content

Force "object"-form when posting json #40

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

Merged
merged 1 commit into from
Apr 1, 2013
Merged

Force "object"-form when posting json #40

merged 1 commit into from
Apr 1, 2013

Conversation

njam
Copy link
Contributor

@njam njam commented Mar 31, 2013

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

stloyd commented Apr 1, 2013

Good catch! Thanks!

@InventisPMTest
Copy link

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

njam commented Apr 25, 2013

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

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

njam commented Apr 25, 2013

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

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