I am running Comment Driven on my website. I am attempting to implement Services for authorized users to post comments to the website. I created a service requiring session authentication. With that service and a valid cookie, I am able to get/create nodes and get comments, but am unable to create new comments.

Sadly, I am able to post a valid comment if I disable Comment Driven (note: I have used a fake username and password for the website):

curl -u username:password -H "Content-type: application/json" -b testCookies.txt http://dev.bouldermountainbike.org/api/testAuth/comment -d '{"nid":"175","comment":"comment_driven disabled, test comment"}'

{"cid":"3351","uri":"http://dev.bouldermountainbike.org/api/testAuth/comment/3351"}

With Comment Driven reenabled:

curl -u username:password -H "Content-type: application/json" -b testCookies.txt http://dev.bouldermountainbike.org/api/testAuth/comment -d '{"nid":"175","comment":"comment_driven now enabled"}'

["An illegal choice has been detected. Please contact the site administrator. Comment field is required (or a driven property change otherwise)."]

This issue is cross-posted to the Services issues queue, with greater issue detail: http://drupal.org/node/1930348

Comments

arhak’s picture

not sure that I will have the time to look deeper into this one,
but I would need something to work with

contact me (via d.o. contact form) to send me some credentials
since I would like to inspect your comment form at http://dev.bouldermountainbike.org/node/175

also state your Drupal version

boulderfuzz’s picture

done.

Note that the Services maintainers state: "This is an issue with comment driven as services simply 'fakes' doing a form submit." in thread http://drupal.org/node/1930348

arhak’s picture

BTW at #1930348: post comment yields "comment field is required" your first chunk of code doesn't seem to correspond with D6:
[...] "comment_body":{ "und":[ { "value":"this test comment [...]

arhak’s picture

instead of posting a nid & comment (via REST) try nid & cdriven-comment
inspecting your comment form you'll notice that it had suffered modifications (by comment_driven) to merge a node form within a comment form

you can decide to implement it like this as a workaround
or you can dig deeper into the matter #741274: Using comment driven programmatically

boulderfuzz’s picture

Re #3
Yup I realized that my comment JSON was poorly formed, and posted a better version later in the thread. When comment_driven was disabled, Services would properly process the following comment POST:

curl -u username:password -H "Content-type: application/json" -b testCookies.txt https://dev.bouldermountainbike.org/api/testAuth/comment -d '{"nid":"175","comment":"comment_driven OFF"}'

{"cid":"3356","uri":"https://dev.bouldermountainbike.org/api/testAuth/comment/3356"}
boulderfuzz’s picture

building off of #4... thanks for the hint, works great!

Thus, with comment_driven enabled, I can successfully use cdriven-comment to post a new comment:

curl -u username:password -H "Content-type: application/json" -b testCookies.txt https://dev.bouldermountainbike.org/api/testAuth/comment -d '{"nid":"175","cdriven-comment":"using cdriven-comment"}'

{"cid":"3360","uri":"https://dev.bouldermountainbike.org/api/testAuth/comment/3360"}
boulderfuzz’s picture

Next stumbling block ... I'll continue under the same issue as this pertains to using Comment Driven with Services 3. BTW I spent time looking through related issues e.g. http://drupal.org/node/746870, http://drupal.org/node/816908 and could not determine how to do the following.

With comment submission, users need to also update the node's field_trail_meter. For example, see form at bottom of http://dev.bouldermountainbike.org/node/217 - Trail Condition corresponds to field_trail_meter.

How do I update this field using Comment Driven and Services 3 REST?

At first I thought I would simply format the POST exactly as formatted when you request the node through Services, and pass through cdriven. For example, here's the appropriate section of node 217, in JSON format as returned by Services:

curl -u username:password https://dev.bouldermountainbike.org/api/testAuth/node/217
....
    "field_trail_meter": [
        {
            "value": "1"
        }
    ],

Attempting to submit the new Trail Condition using cdriven-field_trail_meter:

curl -u username:password -H "Content-type: application/json" -b testCookies.txt https://dev.bouldermountainbike.org/api/testAuth/comment -d '{"nid":"217","cdriven-comment":"using cdriven-comment, attempting to set meter=0","cdriven-field_trail_meter":[{"value":"0"}]}'

{"cid":"3365","uri":"https://dev.bouldermountainbike.org/api/testAuth/comment/3365"}

Looks great at first but there's a catch - the comment was submitted successfully, but the node's field_trail_meter is unchanged. Do I have the cdriven field name incorrect?

arhak’s picture

@#7

Attempting to submit the new Trail Condition using cdriven-field_trail_meter

no, no, the cdriven prefix is NOT for everything

note there are two forms merged into one,
you have to distinguish between what is addressing the comment_form part versus what is aiming the node_form part
see #741274-1: Using comment driven programmatically

PS: when referring to other issues, please type:
- the issue number
- pefixed by #
- and all that between square brackets
that way you write less, the issue gets linked automatically, and we can read the issue title and status in a peek

e.g.
- writing: #746870: FAQ: what might be the problem with comment-related modules? & #816908: Why add "cdriven" prefix to all the names?
- yields: #746870: FAQ: what might be the problem with comment-related modules? & #816908: Why add "cdriven" prefix to all the names?

arhak’s picture

to make my self clear, your use case is not the same as in #741274-1: Using comment driven programmatically, that is an example to distinguish between which fields get prefixed (those belonging to the comment form) and which fields doesn't (those belonging to the node form)

therefore, you should address your field_trail_meter field as if you were submitting it to a node edit form

boulderfuzz’s picture

Thanks for the tutorial on how to properly reference other issues. And thanks for helping me work through this issue in detail.

If I understand you properly, the following should work:
curl -v -u user:pass -H "Content-type: application/json" -b testCookies.txt https://dev.bouldermountainbike.org/api/testAuth/comment -d '{"nid":"217","cdriven-comment":"using cdriven-comment, attempting to set meter=0","field_trail_meter":[{"value":"0"}]}'
... but this syntax still fails to update the node field.

Are you recommending that I do two submissions, one through /comment resource and one through /node resource in Services? I'm trying to avoid that route if at all possible because then I have dissonance between the node versions. It would be best if I could force Services to accept the comment and node updates at once.

arhak’s picture

Are you recommending that I do two submissions, one through /comment resource and one through /node resource in Services?

not at all

... but this syntax still fails to update the node field.

to figure out the proper structure to POST, you should conduct a test on a node edit form first,
once you have a successful REST POST on a node edit form (noting the field you're addressing did change as intended)
then use the same structure for that field on the comment form POST

I'm not telling you to do 2 submissions to achieve your goal,
but to test first with a node edit form, and once you know how to address that field
proceed to do it via comment form

note that deppending on the type of the filed, the structure of field_trail_meter might requiere a different post data
the way to figure that out is conducting a test on the node form itself

arhak’s picture

Category: bug » support
Status: Active » Postponed (maintainer needs more info)
boulderfuzz’s picture

Status: Postponed (maintainer needs more info) » Active

I'm back, sorry but I was ill.

I used your test from @#11 to determine the node form's needs, and now know the syntax to successfully update nodes through Services. However, I discovered that Services requires HTTP PUT to update a node (change a field in already-existing node) but we know from earlier work in this thread that HTTP POST is needed to create a new comment. The crux: I cannot reconcile the PUT edit node and POST new comment into a single call to Services. I will demonstrate below to show you what I mean....

Start by saving the current node fieldset, so I can monitor what changes:
curl -u user:pass -b cookieAdmin.txt https://dev.bouldermountainbike.org/api/testAuth/node/390 > trail390.json

Use Services to submit node edit form via PUT; change node field field_trail_meter (note the node form requires "type" field), this will successfully change field_trail_meter as well as update version number, etc:

curl -v -X PUT -H "Content-Type: application/json" -u user:pass -b cookieAdmin.txt https://dev.bouldermountainbike.org/api/testAuth/node/390 -d '{"type":"trail","field_trail_meter": [{"value": "2"}]}' 

PUT /api/testAuth/node/390 HTTP/1.1
HTTP/1.1 200 OK
{"nid":"390","uri":"https://dev.bouldermountainbike.org/api/testAuth/node/390"}

curl -u user:pass -b cookieAdmin.txt https://dev.bouldermountainbike.org/api/testAuth/node/390  > trail390_nodeChange.json

diff trail390.json trail390_nodeChange.json
8c8
<     "changed": "1334847035",
---
>     "changed": "1365723602",
15,16c15,16
<     "vid": "10297",
<     "revision_uid": "54",
---
>     "vid": "12436",
>     "revision_uid": "18",
21c21
<     "revision_timestamp": "1334847035",
---
>     "revision_timestamp": "1365723602",
48c48
<     "field_trail_meter": [{"value": "1"}],
---
>     "field_trail_meter": [{"value": "2"}],

Changing field_trail_meter was successful! So I know that syntax is correct.

Note that attempting REST POST to edit the node will fail:

curl -v -X POST -H "Content-Type: application/json" -u user:pass -b cookieAdmin.txt https://dev.bouldermountainbike.org/api/testAuth/node/390 -d '{"type":"trail","field_trail_meter": [{"value": "2"}]}'

POST /api/testAuth/node/390 HTTP/1.1
HTTP/1.0 404 Not found: Could not find the controller.

Going back to creating a comment ... as in @#6, I can use Services to create a new comment via POST:

curl -v -X POST -H "Content-Type: application/json" -u user:pass -b cookieAdmin.txt https://dev.bouldermountainbike.org/api/testAuth/comment -d '{"nid":"390","cdriven-comment":"using cdriven-comment, no node changes attempted"}'

POST /api/testAuth/comment HTTP/1.1
HTTP/1.1 200 OK
{"cid":"3374","uri":"https://dev.bouldermountainbike.org/api/testAuth/comment/3374"}

curl -u user:pass -b cookieAdmin.txt https://dev.bouldermountainbike.org/api/testAuth/node/390  > trail390_addComment.json

diff trail390_nodeChange.json trail390_addComment.json
81,83c81,83
<     "last_comment_timestamp": "1361811897",
<     "last_comment_name": "AB",
<     "comment_count": "7",
---
>     "last_comment_timestamp": "1365724961",
>     "last_comment_name": "",
>     "comment_count": "8",

Ultimately I want to update field_trail_meter while creating (POST) a new comment. But this doesn't work; note that although I receive HTTP 200 response for the below, only the comment is updated; field_trail_meter remains unchanged:

curl -v -X POST -H "Content-Type: application/json" -u user:pass -b cookieAdmin.txt https://dev.bouldermountainbike.org/api/testAuth/comment -d '{"nid":"390","cdriven-comment":"using cdriven-comment to POST comment, attempting to change field_trail_meter through node form","type":"trail","field_trail_meter":[{"value": "1"}]}'

POST /api/testAuth/comment HTTP/1.1
HTTP/1.1 200 OK
{"cid":"3375","uri":"https://dev.bouldermountainbike.org/api/testAuth/comment/3375"}

curl -u user:pass -b cookieAdmin.txt https://dev.bouldermountainbike.org/api/testAuth/node/390  > trail390_commentAndMeter.json

diff trail390_addComment.json trail390_commentAndMeter.json
81c81
<     "last_comment_timestamp": "1365724961",
---
>     "last_comment_timestamp": "1365725842",
83c83
<     "comment_count": "8",
---
>     "comment_count": "9",

Ideas on how to proceed?