10

I'm having trouble getting services to recognize a POST to /comment.json. I have a similar issue with creating nodes as well.

The following is the error I get: HTTP/1.1 406 Not Acceptable: Missing required argument comment

This message comes from the file: /services/server/rest_server/includes/RESTServer.inc Specifically, the lines:

  elseif (!isset($info['optional']) || !$info['optional']) {
    return services_error(t('Missing required argument !arg', array(
      '!arg' => $info['name'],
    )), 406);
  }

basically, it seems to think the "comment" argument is missing (and it's not optional).

This is one of many bits of JSON I've given it (posting a comment to node 8801)

{name:"test",language:"und",cid:"0",pid:"0",uid:"17593",nid:"8801",
comment:{subject:"Subject of the comment, if not entered it will be auto-generated",
comment_body:
{und:[
[{value:"value of the field comment in the comment, remember that in D7 we can create fields for comments too", format: "full_html"}]
]}}}

It's perfectly valid (based on other online JSON checks). It also matches what I see in the services test examples at /services/test/functional/ServivesResourceCommentTests.test

however, I always get the same error message. This seems similar to: Drupal 7 services json node object

which is also unresolved. Can someone help me with the proper JSON formatting? Really, all I want to do is pass it something like:

{ nid : 8081 , uid : 17593 ,comment:{comment_body:"test",subject:"test"}}

my http headers seem ok and user/login worked fine.

5 Answers 5

5

The following JSON appears to be the minimum you need to post a comment:

{
  "nid":"1",
  "subject":"Comment Subject Text",
  "comment_body":{
    "und":[{
      "value":"Comment body text"
    }]
  }
}

Since there is no user, commenting on this "example.com/node/1" needs to be turned on and permissions for anonymous users to post comments need to be set.

2

Turns out I was trying to create nodes/comments without authentication, and the site didn't allow anonymous users to create nodes/comments. Once I turned on session auth, logged in properly, things worked fine.

1
  • 1
    So you turned on session auth in Drupal services or in your REST client? Can you give some quick tips on how you did this? Commented Jun 18, 2012 at 14:58
2

Can you tell me to what URL you would post this json string. I've been looking all over the web for some documentation how to create a comment on a node using services 3. The documentation on this module is completely lacking. I've tried posting to:

example.com/api/node/{nid}/comments.json

example.com/api/comment.json

example.com/api/comment/create.json

Nothing seems to work. I always get "404 Not Found : Could not find controller"

EDIT: as soon as I posted this I got it to work. If it will help anyone else you post your json string to:

example.com/api/comment.json

If you're using Poster to test be sure to set the content type to application/json. When using jquery make sure you set dataType: 'json'.

2

Here are some examples to create and update nodes and comments in the following format:

  • HTTP method
  • ContentType
  • URL Path
  • Data to Send

Node Create

  • POST
  • ContentType: application/json
  • ?q=endpoint/node.json

    {"type":"article","title":"t8yZUfX2it","language":"und"}

Node Update

  • PUT
  • ContentType: application/json
  • ?q=endpoint/node/123.json

    {"node":{"nid":"123","title":"9AjrXJhWMI","language":"und"}}

Comment Create

  • POST
  • ContentType: application/json
  • ?q=endpoint/comment.json

    {"subject":"liJmJfpqMX", "comment_body":{"und":[{"value":"9DyaNZi3lA"}]}, "nid":"18"}

Comment Update

  • PUT
  • ContentType: application/x-www-form-urlencoded
  • ?q=endpoint/comment/456.json

    { cid: 456, "subject":"hzU27R6daE", "comment_body":{"und":[{"value":"BqZU5DwArD"}]}, "nid":"18"}

1
  • 1
    This works well. Should be the accepted answer! one thing to remember is to add the key:"Content-Type" value:"application/json" to your header if you are testing from Chrome postman. This is another invaluable resource: https://www.drupal.org/node/1447020 Commented Apr 7, 2016 at 14:23
1

I was getting similar problems even when just testing with the firefox plugin poster. It turns out that it was quite simply the extra whitespaces and some other simple formatting (such as escapes) that was doing it. Here is a list of what works in my own tests for what I needed:

CREATE USER WORKS = endpoint/user

{"name":"myusername","pass":"12345678","mail":"[email protected]","status":"1"}

LOGIN WORKS = endpoint/user/login

{"username":"admin","password":"admin"}

CREATE FILE WORKS = endpoint/file

{"uid":"1","filesize":"99999","filename":"whatever.bin","file":"BASE64_ENCODED_DATA"}

CREATE NODE WORKS = endpoint/node

{"title":"testrest1","type":"dl","field_dl_file":{"und":[{"fid":582}]},"uid":"1","language":"und"}

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