Simple use of Services with JSON

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by ilovedrupal-gdo on June 18, 2009 at 7:05pm

I am trying a simple test with Drupal 6, Services, JSON Server but am running into the following error. To make things as simple as possible, I tried this with the curl command line program (although I have also tried it in python and php). I have services and json server installed and can go to services at mysite.com/admin/build/services/browse/service.name and successfully browse a view I made using views.get. I also did this with node.get However (all on the same machine) when I try:

curl --data method=views.get --data view_name="myview" http://mysite/services/json

or

curl --data method=system.getServices http://mysite/services/?q=services/json

or

curl --data method=system.connect http://mysite/services/?q=services/json

etc...

I always get:

{ "#error": true, "#data": "Invalid method " }

http://drupal.org/node/239806

although I see that is it using a service called echo.

Can anyone help with this?

Thanks very much

Categories:

Comments

Any luck with this?

Posted by yajnin on July 17, 2009 at 11:09pm

Any luck with this?

Works for me

Posted by wendelj on September 30, 2009 at 4:34am

I tested out the last two examples against my server and they worked fine.

Output from last one since it's short.

curl --data method=system.connect http://{mysite}/services/?q=services/json

{ "#error": false, "#data": { "sessid": "45572a32241703b5c7fd0f03d1d8466f", "user": { "uid": 0, "hostname": "{ip removed}", "roles": { "1": "anonymous user" }, "session": "", "cache": 0 } } }j

do you still have trouble?

Posted by pbharrin on October 6, 2009 at 7:48pm

I would leave out the first services ie:
http://{mysite}/?q=services/json

I still have troubles

Posted by wira.perdana on October 13, 2009 at 12:49am

I always get: { "#error": true, "#data": "Invalid method " } in any cases.

http://{mysite}/?q=services/json
http://{mysite}/services/?q=services/json
http://{mysite}/services/json
http://{mysite}/services/services/json

none of the methods work.

any suggestions?

Sorted it...

Posted by nicholasthompson on November 1, 2009 at 10:54pm

vmtest6 is the name of one of dev sites. The key seems to be to wrap system.connect in quotes to make the json decode function treat it as a json string.

[nthompson@localhost ~]$ curl --data 'method="system.connect"' http://vmtest6/services/json
{ "#error": false, "#data": { "sessid": "01add0fjfgrql7ntf4p1211p94", "user": { "uid": 0, "hostname": "192.168.216.128", "roles": { "1": "anonymous user" }, "session": "", "cache": 0 } } }

same problem.. I always get {

Posted by upupax on November 4, 2009 at 5:58pm

same problem..
I always get { "#error": true, "#data": "Invalid method " }

This worked for me: curl

Posted by burningdog on February 3, 2010 at 8:41pm

This worked for me:

curl --data 'method="my_module.connect"' --data 'name="Roger Saner"' --data 'mail="me@example.com"' http://example.com/?q=services/json

The 'name' and 'mail' variables are arguments.

Enabling more modules

Posted by dodwyer on August 17, 2010 at 8:09am

I found I had the same issues until I enabled the additional service modules, System Service, Views Service etc

Hope this helps.

Fixed with jQuery

Posted by nirvisual on August 26, 2010 at 4:27pm

I kept getting 'invalid method' errors for all my requests using jQuery. A few things solved my problem:
1. Use $.post() instead of $.getJSON. The JSON Server module only answers POST requests.
2. As Nicholas said above, you have to quote EVERYTHING in a string. So for instance:
$.post('/services/json', 'method: "system.connect"')

Here's a little function set that will help you connect via jQuery:

function drupal_json_str(data){
var request_str = ''
for (arg in data){
request_str += arg+'='+'"'+data[arg]+'"&'
}
return request_str
}

function drupal_json_request(data){
return $.post('/services/json', drupal_json_str(data))
}
// Now you can call it like you THINK you should... with a frickin object
drupal_json_request({method: 'system.connect'})

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /