We're using services to make a API available to a third party website which registers new users on our Drupal instance. To check if the user already exists it will attempt to fetch to use user/index to check the user by e-mail and retrieves the UID.

I've tried to accomplish the latter, but I get strange results from the API:

http://site.loc/api/user/index?fields=name,uid&api-key=API-KEY

---
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |

Any idea what's causing this? Thanks!

Comments

Ambidex created an issue. See original summary.

nvaken’s picture

Issue summary: View changes
tyler.frankenstein’s picture

Status: Active » Postponed (maintainer needs more info)

It looks like you may have some bogus entries in your users table. I can see 20 empty results, and 20 results is the default number returned by the entity index resources. I'd dig through your users table to see if there are any bogus rows in the table.

Also, there is no filter on your query string, so that'll always return the first 20 results from the users table. See this issue for advanced filter usage: #2537968: Improve flexibility of entity index resources

nvaken’s picture

Hi tyler.frankenstein! Thanks for the lightning fast response, mucho appreciated.

You might be right that there are bogus entries in there, though, I tried a few others. Would you agree that the following should contain a valid response? I use uid 1 as account to maintain the site all the time and should at least return an e-mailaddress or uid.

http://site.loc/api/user/index?parameters[uid]=1&fields=name,uid&api-key...
Returns:

---
- |
tyler.frankenstein’s picture

@Ambidex, you're welcome.

Yes, the query string in your above example should return the name and user id for user # 1. Perhaps try temporarily disabling the api-key config and removing it from the URL to see if that makes a difference. Other than that, I'd first suspect any contrib/custom modules that tinker with Services calls/hooks: http://cgit.drupalcode.org/services/tree/docs/services.alter.api.php?h=7...

Then I'd look internally to the Services module to see how/why the results are being assembled/returned that way: http://cgit.drupalcode.org/services/tree/resources/user_resource.inc?h=7...

nvaken’s picture

I've debugged all the services alter hooks listed in that first link by running `dpm(module_implements('HOOK_NAME'))`. The only hook that is being implemented is `hook_services_request_preprocess_alter` by `services_views` but I cannot imagine that that would be the culprit.

Also, I've checked the `_user_resource_index` function and what it returns. When I check the results of the `services_resource_build_index_list($results, 'user', 'uid');` function, I get back a decent list of user objects with all their properties, so, I'm still a bit in the dark about what's going on...

nvaken’s picture

Status: Postponed (maintainer needs more info) » Active
tyler.frankenstein’s picture

Try this URL:

http://site.loc/api/user?fields=name,uid&api-key=API-KEY

Instead of this URL:

http://site.loc/api/user/index?fields=name,uid&api-key=API-KEY

And if you're looking for JSON output, I'd recommend replacing "user" with "user.json" in the URL.

nvaken’s picture

Woah, I had just tracked this thing down to the Spyc::YAMLDump($data); line in ServicesFormatter.inc in which $data goes in with the correct results but the results that Spyc::YAMLDump() returns is the above output. So, it seems that Spyc is not able to process the data?

Your JSON suggestion above is also a life saver, because, when using `user.json` the output seems to be just fine! Does this mean the Spyc implementation is broken? I do not have any experience with the Spyc library.

tyler.frankenstein’s picture

Status: Active » Closed (works as designed)

I never quite figured out what SPYC was for, and I've successfully used the .json extension for years now and have never worried about what SPYC is up to, I think it may be for other formats, xml, etc.

I'm assuming the .json has solved your problem, so I'll go ahead and close this, re-open if need be, thanks for the follow up.

nvaken’s picture

Spyc is used for YAML formatting. I'm not really sure if it didn't work because of environmental issues or if it's Services implementation is flawed, in the latter it would mean that this is a bug within Segices and this issue might need to be reopened. But I guess it's good for now, my single occurrence of this issue does not prove a pattern.

For my case, I'm happy with the .json format and I'll leave this issue closed. 😉 Thanks for the help!