Posted by eimhee on April 23, 2011 at 9:19am
I am try to save a node with service3.0, but it didn't worked,
I am using restful style, is there any example to save a node using service3?
public Object nodeSave(MultivaluedMap<String, String> queryParams) {
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost/drupal/rest/node/create?XDEBUG_SESSION_START=ECLIPSE_DBGP&KEY=13033148613441");
Cookie cookie = getCookie();
ClientResponse response = webResource.accept("application/json").cookie(cookie).put(ClientResponse.class, queryParams);
int status = response.getStatus();
String textEntity = response.getEntity(String.class);
System.out.println(textEntity);
if (Response.Status.OK.getStatusCode() == status) {
textEntity = response.getEntity(String.class);
System.out.println(textEntity);
}
return null;
}
Comments
MultivaluedMap<String,
What I can see wrong (even I
What I can see wrong (even I don't know the language your code is written) is that you do call to wrong url. In order to create the node you need to do POST call to host/endpoint/node url.
Please use services module tests of the node resource (tests/functional/ServicesResourceNodeTests.test) as reference on how to create the node.
Thank you for your comment,
Thank you for your comment, I am using java language , I have a simple question?
How do I run the test case ServicesResourceNodeTests.test?
I think I should use PUT call to endpoint if I want to create a node, what is the different between PUT and POST call?
I believe you work with
I believe you work with drupal 7. Then enable testing module. Then in configuration you can find Testing. There you can choose Services tests to run.
On drupal 6 you need to install http://drupal.org/project/simpletest module
Difference PUT and POST calls is that there calls are different request methods. You can read about them here http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
POST if to create a node.
PUT is to update it.
Where is the "Testing"
Where is the "Testing" configuration that you are seeing? I'm using 7.12 and see no such thing (after enabling the services test resource module)
Thanks ygerasimov I used
Thanks ygerasimov
I used Poster tool in firefox to test webservice ,I get a 406 error,
URL : http://localhost/drupal/rest/node
Content Type:application/json
{
{ 'node[title]' : 'testing'},
{'node[body][UND][0][value]' : 'bodytest'},
{'node[type]' : 'article'},
{'node[language]' : 'UND'},
{'node[name]' : "1"}
}
Response:
Status:406 Not Acceptable:Missing required argument node
how to set the body of the
how to set the body of the node , I found the body of the node have not been saved.
{"node":{"title":"title2","body":"body ","type":"category"}}
Hi eimhee, I was struggling
Hi eimhee,
I was struggling with the same thing.
You need the set the body like below:
{"node":{
"type":"article",
"title":"This is my new title ",
"language":"und",
"body":{"und":{"0":{"value":"This is the body of my node"}}}
}}
The body field doesn't accept strings like the other fields. It's a multidimensional array like:
node['body']['und']['0']['value'] = 'Your body text'.
You can see the arrays when you install the devel module and go the the devel tab on a node.
www.braahm.be
I found
I found http://drupal.org/node/1206272 it doesn't work for me, at least, not in a native way using Titanium. I had to add an external library to make it work.
Luis
in 7.x-3.x you should pass
in 7.x-3.x you should pass body in following way (code in php from test):
$node = array('title' => 'testing',
'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomString()))),
'type' => 'page',
'name' => $this->privilegedUser->name,
'language' => LANGUAGE_NONE,
);
how to represente node body
how to represente node body in json ?
Basically it's like the
Basically it's like the following:
{"node":{"type":"article",
"title":"This is my new title ",
"language":"und",
"body":{"und":{"0":{"value":"This is the body of my node"}}}
}}
See my reply above.
Cheers,
www.braahm.be
it doesn't work. having: var
it doesn't work. having:
var node = {"type":"article",
"title":"This is my new title ",
"language":"und",
"body":{"und":{"0":{"value":"This is the body of my node"}}}
};
creates the node but the body will not be created., only the title.
If I use PHP to create a node using services and I encode the node object in JSON, I get
{"title":"Title goes here","body":{"und":[{"value":"This is the body"}]},"type":"article"}but that doesn't work either, even though the node is saved even with the body.
Can someone post an answer please?
Luis
Hmmmm....
Not really sure here, never did any heavy node manipulation through the Services modules, but if it's anything like typical node setup, shouldn't it be:
var node = {"type":"article",
"title":"This is my new title ",
"language":"und",
"body":{"und":[{"value":"This is the body of my node"}]}
};
The zero (0) in body['und'] is an indexed array if i'm not mistaken.
the biggest issue here is
the biggest issue here is that comments follow the same pattern for fields, so, basically I'm looking to solve two problems.
Luis
It worked for me with this
It worked for me with this format and some changes in the module:
node={"title":"the title","type":"trip","field_price":{"es":[{"value":1200,"format":null,"safe_value":1200}]}}
node={"title":"the title","type":"trip","field_price":{"es":[{"value":1200}]}}
The first line is the one returned by the GET method, and the price is a CCK field.
I've tried it with the version 7.x-3.0-rc2 and it's pretty tricky. First I had 401 errors with the auth because for some reason the services_sessauth module was gone. Then I had a 406 because the param was not treated as JSON in _node_resource_access and _node_resource_create so the type wasn't read.
I'm not sure if that's due to my call but even when those errors are fixed it fails in field.atach.inc -> _field_invoke because the object there doesn't look as expected. A decoded JSON is not valid and an array doesn't work either, the format for the associative array and object is the same in JSON so I don't see any way to send a JSON with that format without changing the code. This worked for me although I wonder if there is an easier way:
function _node_resource_create($node_init) {
$node = json_decode($node_init[0]);
if (!isset($node->type)) {
return services_error('Missing node type', 406);
}
// Load the required includes for drupal_execute
module_load_include('inc', 'node', 'node.pages');
// Setup form_state
$form_state = array();
$form_state['values'] = json_decode($node_init[0],true);
$form_state['values']['op'] = t('Save');
// Wanted to return a gracefull error instead of a blank nid, this should
// allow for that.
$types = node_type_get_types();
$node_type = $node->type;
if (isset($types[$node_type])) {
$custom_node = membersAsArray($node);
drupal_form_submit($node_type . '_node_form', $form_state, $custom_node);
}
else {
return services_error(t('Node type @type does not exist.', array('@type' => $node_type)), 406);
}
if ($errors = form_get_errors()) {
return services_error(implode(" ", $errors), 406);
}
// Fetch $nid out of $form_state
$nid = $form_state['nid'];
// Only add the URI for servers that support it.
$node = array('nid' => $nid);
if ($uri = services_resource_uri(array('node', $nid))) {
$node['uri'] = $uri;
}
return $node;
}
function membersAsArray($object)
{
foreach($object as $member=>$data){
$object->{$member} = json_decode(json_encode($data), true);
}
return $object;
}
I have the same issue
Is there any clear documentation on what the argument structure should be for node.create?
This gives me a 401 error:
def args = [ title:'Sample Auto Page', body:[und:value:'This is a test!'] ,
type:'my_page_type', field_xt:[und:value:'Retail'] ]
def c = serverProxy.invokeMethod ('node.create', [args]);
Any help would be appreciated.
thanks,
You need to pass the
You need to pass the credentials in a cookie, like this (with curl) :
curl http://theserver.com/theservice/node -d"&type=standard&title=testnode2&body[und][0][value]=bodytestnode" -v -H "Cookie:SESSc37abbbbd4842738504c13fe0d543783=7mq0nvCgg6SXqqvdniUE_2WY4YRppKfqiQ0nk-axw2I"
... where the session info comes from a previous authentication :
curl http://theserver.com/theservice/the_alias/login -d"username=yourname&password=yourpass -v (in my case, I aliased the user service, your mileage may vary)
I managed to create nodes this way, now strugling with updates ...
Node update
Figured it out :
curl http://theserver.com/the_service/node/41 -X PUT -d"&type=standard&body[fr][0][value]=Countzero was here" -v -H "Cookie:SESSc37abbbbd4842738504c13fe0d543783=7mq0nvCgg6SXqqvdniUE_2WY4YRppKfqiQ0nk-axw2I"Please help, how to populate fields Services 3 Drupal 7
I'm banging my head against the wall trying to get this to work, I'm trying to get a titanium application to create a node, with a number of fields. I'm using poster (for firefox) to test, I'm able to get it to create a node like this
{"uid":"1",
"title":"My Title",
"type":"my_type",
"field_lat":
{"und":[{"value":"40.703806"}]}
}
This works in poster, and the node is created with the correct field, when try and post using titanium, I trace out the parameters in the http request and I get the exact same string, only when I try and create the node from the titanium app, it ignores the "field_lat" but still created the node with all the other fields, I'm really stumped, I have no idea why this is happening can anyone help me trouble shoot?
Turned out I was not setting
Turned out I was not setting the request headers correctly, drupal did not know it was JSON so it never called the JSON decode method
Deleted...
Deleted...
@dereckd can you please post
@dereckd can you please post the complete code?
Luis