0

I have successfully installed the Services module and created a service with which I can retrieve a node or even several nodes (using Views Services) I've now configured an endpoint, whose purpose is to allow an application to create nodes through a JSON call.

After much messing about I finally configured the following JSON call:

{
"type":"demande-de-rdv",
"uid":"0",
"language":"und",
"status":"1",
"promote":"0",
"sticky":"0",
"title":"JSON Post using http_request",
"body":"Morbi sit amet leo molestie, luctus metus aliquet, pellentesque lacus! Suspendisse at laoreet nulla. Nunc sed tincidunt dolor. Donec condimentum     odio non nisl condimentum, id euismod arcu rhoncus? Nullam sit amet ante convallis, viverra nulla in, laoreet leo. Fusce vitae iaculis libero. Phasellus scelerisque varius augue, quis vulputate sapien vehicula vel. Sed at est at felis dignissim rutrum. Sed porta tristique sapien et viverra. Nunc ac elit tristique, laoreet lacus at, pharetra elit. Morbi sit amet commodo dolor? Donec consectetur justo vel fringilla rutrum"
}

However, when I post the call I get the reply "403 : Access denied for user anonymous", this despite the fact that I have configured the content type to be creatable by Anonymous (and have created a node for the content type in question to test it). I also gave Anonymous all the permissions on the Services module just in case, but it made no difference.

Where i am going wrong?

6
  • 1
    Try remove sessions authentication from that endpoint Aug 3, 2015 at 15:02
  • Don't see how I can do that. Under the "Authentication" tab, I get this message: "No authentication modules are enabled, all requests will be anonymous"
    – Martin K
    Aug 4, 2015 at 15:49
  • I have posted to <endpoint>/node and it works for anonymous after granted that permission and cleared cache by the way you don't have to set the field uid and it's not necessary Aug 4, 2015 at 21:37
  • Your body field is formatted incorrectly no matter what. It should be body: { und: [ { value: 'hello world' } ] }, also try removing the status, promote and sticky properties, those are usually reserved for admins. Aug 5, 2015 at 12:03
  • @tyler.frankenstein - I did as you suggested and got a step forward (I think) inasmuch as now my service returns "401 Unauthorized : Missing required argument node". I tried modifying the request by putting {node[...]} around the whole, but no luck. Is there anywhere where the proper format is documented?
    – Martin K
    Aug 24, 2015 at 9:23

1 Answer 1

0

I have posted to /node and it works for anonymous after granted that permission and cleared cache by the way you don't have to set the field uid and it's not necessary if you set

$request_url = 'domain/endpoint/node';

// Node data
$node_data = array(
  'title' => 'product testing',
  'type' => 'product',
    "uid" => 0,
    "language" => "en",
    "sticky" => 0    
);
//$node_data = http_build_query($node_data);
$data = json_encode($node_data);

// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json')                                                                       
);
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, FALSE);  // Ask to not return Header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);

$response = curl_exec($curl);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.