I've been working on a RESTful API, and having difficulty putting together a reference implementation that I can expand to the entire API I want.
Basing my work on the 'noteresource' module that Hugo has at https://github.com/hugowetterberg/services-3.x-sample, I made that into a proper module and found that the POST & PUT operations did not work. Thinking I may have screwed something up, I reduced the 'noteresource' module into a very bare framework, renaming it 'api_shell' and adding comments as a form of documentation as I figure things out. (I removed the database logic, and reverted to just 'simulating' such operations. All I really want is to see the API testing logic reach my services callbacks.)
Anyway, attached to this are zipped versions of both the api_shell and api_shell_test modules.
Instructions for how to setup this and run the tests are at the top of api_shell.module and api_shell_test.js. It is very similar to Hugo's original logic, but with a few changes such as the logic requires being launched from FireBug's javascript console like this:
gApiShell.note.test()
Additionally, there is a gApiShell.note.testRetrieve() for manually testing a Retrieve operation because in Hugo's original logic that was only called when a POST or PUT operation succeeds.
If anyone who has POST or PUT or Targeted Actions working in their code could take a glance, I would really appreciate it.
Once this is working, it will be a working REST api shell others can base their work upon. I intend to add these to the Services Handbook when they are working.
| Comment | File | Size | Author |
|---|---|---|---|
| api_shell_example.zip | 10.44 KB | bsenftner |
Comments
Comment #1
bsenftner commentedI just tried moving to the nightly development snapshot from Jan-19th,2011.
I am seeing the same items working and the same items failing as outlined in the attached modules. (POST, PUT, and Targeted Actions don't work.) (Come to think of it, I don't have any Relationship example implemented yet...)
Comment #2
kylebrowning commentedHrmm, POST and PUT both work for me, IM able to POST a new node, and PUT the same node with updated fields. The Comments resource has perfect examples of both PUT (update) and POST(new and actions). THe URL im using to access a targeted action would be, comment/loadNodeComments and passing an NID, the only non optional parameter. Can you explain whats not working for you and why this core resource isnt working with POST and PUT? It should be a perfect example.
Comment #3
bsenftner commentedThanks for your comment, Kyle.
I'm seeing the Services Endpoint being hit and recognized as a Services Endpoint when I check 'recent hits' at admin/reports/hits. This is while trying POSTs or PUTs, but my Services callback is not being called.
Focusing only on POST for the moment, I was thinking the problem may be access permissions. (Even though I'm the super-user.) So I changed my Create operation's definition to call an access callback of my own:
Here's the access callback, only slightly different from Hugo's original:
(both GET & DELETE operations show this access callback being called, but neither PUT nor POST operations hit it)
And finally, this is how I am doing a POST in javascript:
Other than slight changes, this is the same logic as Hugo's example in noteresource.js.
When executing a POST, I'm seeing the FireBug js console output "'inside note.create, about to POST to api_shell/dev/note", so that much looks correct. Checking admin/reports/hits, I see "api_shell/dev/note" as my site's last hit and it is labeled as a Services endpoint. But from there, my access callback is not being called, and I'm not sure where to look next...
Where would I find the 'entry point' where Services receives a raw POST or PUT? I'll trace it from there. My PUTs and POSTs are being seen as Service Endpoint hits, so I should be able to trace through the completely unfamiliar to me source and figure out where I'm being discarded...
Comment #4
bsenftner commentedTaking a look at the Services source for the first time, I realized that watchdog entries were being logged. Taking a look at the watchdog entry for my last POST attempt, I see:
So, it looks like getControllerArguments($controller, $path, $method, &$sources=array()) is barfing on my POST, looking for a missing 4th parameter
So, adding some print statements to the start of getControllerArguments() to simply show the input parameters, I'm seeing:
However, looking over what I'm doing in my javascript, I'm not seeing where my bug is... as you can see in the last code block of my #3 post, how I do my POST is exactly from Hugo's example...
Comment #5
bsenftner commentedWell, I got POST working!
Very surprising, the bug was that I'd changed the Create operation's argument type to 'array' from 'struct' (probably a typo) and after changing it back to 'struct' my POST operation worked!
Should that be a cause of failure?
Tracking this same possible solution for my PUT operations, I also had the same typo that changed my PUT's argument type from 'struct' to 'array', which I've switched back to 'struct' but am still not getting my Update operation's callback being hit. But it looks like I have good momentum here... The same watchdog error is being triggered, so I can follow that. (it's late here. tomorrow...)
Comment #6
kylebrowning commentedClosing this as were handling it in another thread, #1038794: possible bug in getControllerArguments() in RESTServer.inc doing a PUT