I don't get it: the project page for json_server states it won't be necessary for services 3.x because it has json built-in.

I've setup services 3.0-rc1 and am unable to create a JSON server. When I look in the code I don't see a json server anywhere either. Is the information on the project page perhaps outdated? Am I overlooking something?

Comments

welly’s picture

If you set up a service in Services 3 - a REST service and set your end point (say api) - if you do http://example.com/api/node/1.json - it'll give you json content for node 1 or http://example.com/api/node.json will give you json content for a bunch of nodes. That's all I have figured out so far :)

spazefish’s picture

Drupal 7 services 3 json , I put that there so people can find it with google easier.
I am having the same issues, with the REST thing. It is very unclear how the json thing works, does it let me create an object in JSON and send it to my api/service endpoint and it recieves it and interprets it similar as to how json server does it?

I don't think so, at least I can't get it to work yet, it's not like the json server, but oddly enough on the project page for json server it says json server will not be developed for drupal 7 because services 3 support json out of the box, which is semi-true I suppose? I mean you can perhaps retrieve with json, but if you develop web app with javascript it would be extremely useful to be able to send json as well, for consuming these webservices. Right now, I don't think you can.... please prove me wrong though! Cause this is exactly what I want ;-)

Afaik there are no examples at all in the services 3 manual for this, or how it works with json... :/

askibinski’s picture

Yeah, I reverted back to services 2.x with json_server for the time being. Looking at the comment in #1 json might be a very well undocumented feature of services 3?

spazefish’s picture

Found out how todo this for drupal 7 with Services 3, the answer is yes the REST server does accept json both for sending and receiving.
It's just the Services documentation is awful, completely hopeless, I have been taking shots in the dark to get this working, still can't get comments working, but on the way there.

Ok so to make this work create your endpoint, lets say its http://yourdrupalsite/myendpoint
Now to create a new node with javascript you will do something like: (Provided you have activated the node resource etc)

  var obj={
    node:{
      title: 'New node test',
      type:'article', //Whatever your content type is..
      language:'en', //this attribute is optional, en for english
      body: { 
        und: [
        {value:'sdasdasd', format:'full_html'} //This is the body, if you have other fields its supposed to be similar, just body would be the field name
        ]
      }
    }
  };

So yes the object in javascript needs to have that exact structure, then you make the post with ajax... if you use jquery like so:

    $.ajax({
      type: "POST", //All creates are done with POST, updates are done with PUT and delete with DELETE
      url: 'http://yourdrupalsite/myendpoint/node', //Yes you need to add /node after your endpoint to specify its the node resource we want to consume
      data: JSON.stringify(obj),
      dataType: 'json',
      contentType: 'application/json',
      success: function(data){
        //Do something success... inspect the data  console.log(data) or something
      }
    });

And obviously it's similar with the other resources, however comments I cant get working, I make the object like so:

  var objCmt={
    comment:{
      body:'testing testing comment1',
      nid: 1
    }
  };

However the response I get is " 406 (Not Acceptable: Comment field is required.) "
The comment JSON is semi correct because if I change the structure it complains about missing argument comment, here it just complains about a missing field, which I dont really know what it is I suppose it might be called differently the "body" of the comment perhaps.

Now to consume the comment resource


    $.ajax({
      type: "POST",
      url: 'http://yourdrupalsite/myendpoint/comment', //Here use slash comment instead of node to point to the comment resource
      data: JSON.stringify(objCmt),
      dataType: 'json',
      contentType: 'application/json',
      success: function(data){
        console.log(data); //Here it complains about the missing field...
      }
    });

Anwyay at last some progress

spazefish’s picture

Got the commenting working as well, the comment object if you pass it from javascript needs to look like this:


  var objCmt={
    comment:{
      comment_body:{
        und:[
          {value:'prueba comment'}
        ]
      },
      nid: 1
    }
  };

Then pass it with the same ajax used above, obviously nid is the node id. Now looking for something to retrieve X comments from a node + some other useful things.

skyredwang’s picture

Status: Active » Closed (fixed)
lelizondo’s picture

Status: Closed (fixed) » Active

I read #4 but is not working for me using Services 3.0

var obj={
	node:{
	title: 'Hello',
	type:'article', 
	body: {
	und: [
	 {value:'Hello World!', format:'plain_text'}
	]
}}};

The response is 406.

However,

var node = {
	"title":"Hello " + user.uid,
	"body":{
		"und":[
		{"value":"Hello World!", "format":'plain_text'}
		]
	},
	"type":"article"
};

Works, the node is saved, BUT, the body is not saved, only the title.

Can someone please help me out with this please?

skyredwang’s picture

Status: Active » Closed (fixed)

json_server only works with Services 2.x

lelizondo’s picture

Sorry about that, I didn't see what the project was, I thought it was Services.

boran’s picture

Sorry to post to a closed issue, but, as I understand it,
the services 3.x json/rest interface only allows CRUD operations, it does not allows custom remote functions e.g. calling service.foo($object) .
Has anyone a suggestion to replace this functionality for D7?