Simple use of Services with JSON

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
ilovedrupal-gdo's picture

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

Comments

Any luck with this?

yajnin's picture

Any luck with this?

Works for me

wendelj's picture

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?

pbharrin's picture

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

I still have troubles

wira.perdana's picture

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...

nicholasThompson's picture

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 {

upupax's picture

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

This worked for me: curl

burningdog's picture

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

dodwyer's picture

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

nirvisual's picture

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'})