Hi Everyone,

I'm trying to post a vote through a REST POST call using json (or hal_json).
The GET request works just fine and I'm able to get the output which is something similar to this:

url: http://localhost:8001/entity/vote/19?_format=json

{
"id":[
{
"value": 19
}
],
"uuid":[
{
"value": "c81b488a-d5a1-49f4-97ea-4681c3248414"
}
],
"type":[
{
"target_id": "like",
"target_type": "vote_type",
"target_uuid": "cacd122f-8bfe-484c-9585-ee64ddb47c3d"
}
],
"entity_type":[
{
"value": "post"
}
],
"entity_id":[
{
"target_id": 52
}
],
"value":[
{
"value": 1
}
],
"value_type":[
{
"value": "points"
}
],
"user_id":[
{
"target_id": 13,
"target_type": "user",
"target_uuid": "94227fde-1ef5-43ef-a229-943d0fd54fa4",
"url": "/en/user/13/stream"
}
],
"timestamp":[
{
"value": "2018-07-16T08:03:28+00:00",
"format": "Y-m-d\\TH:i:sP"
}
],
"vote_source":[
{
"value": "::1"
}
]
}

The problem is that when i try to post a vote on a post/comment by using the following code, it returns an error which is:

422 Unprocessable Entity
and it says this:

"Unprocessable Entity: validation failed.\nentity_id.0.target_id: The referenced entity (node: 70) does not exist.

code used in the post:
url: http://localhost:8001/entity/vote?_format=json
authenticated with X-CSRF-Token

{
"type":[
{
"target_id": "like",
"target_type": "vote_type"
}
],
"entity_type":[
{
"value": "post"
}
],
"entity_id":[
{
"target_id": 70
}
],
"value":[
{
"value": 1
}
],
"value_type":[
{
"value": "points"
}
],
"user_id":[
{
"target_id": 13,
"target_type": "user",
"url": "/en/user/13/stream"
}
],
"timestamp":[
{
"value": "2018-07-16T08:03:28+00:00",
"format": "Y-m-d\\TH:i:sP"
}
],
"vote_source":[
{
"value": "::1"
}
]
}

So basically it is saying that the entity 70th is a node (when in reality it's a simple post) and it doesn't work.
If i try to post a vote on an actual node instead, it works like a charm.

Some suggestions?

Comments

Anonymous’s picture

dinozof created an issue. See original summary.

Anonymous’s picture

Small update !

while trying to debug the problem i found out that if an entity node exists and has the same ID of the comment to which i'm trying to add the like, then it works!

so basically:

if i'm trying to add a like to the post which ID is 15 and exists a node that has the same id (15) then the REST post request get accepted and the likes get added to the correct post.
otherwise it will report a 422 error of Unprocessable entity.

best

dstorozhuk’s picture

dstorozhuk’s picture

Title: REST post on opensocial doesn't work » Voting API REST post on doesn't work, validation error, when referenced entity other than node
dstorozhuk’s picture

Component: Views Integration » Code
StatusFileSize
new9.87 KB
dstorozhuk’s picture

Replace wrong formatted patch.

dstorozhuk’s picture

In general - we need to lock the entity type witch which this vote could be used. My patch is about that.

dstorozhuk’s picture

dstorozhuk’s picture

Title: Voting API REST post on doesn't work, validation error, when referenced entity other than node » Voting API REST POST (create with REST) doesn't work, validation error, when referenced entity other than node
pifagor’s picture

Status: Active » Needs review
tr’s picture

Version: 8.x-3.0-alpha5 » 8.x-3.x-dev
Assigned: » Unassigned
Issue tags: -OpenSocial, -votingapi, -drupal8
tr’s picture

Status: Needs review » Needs work

Patch no longer applies. Needs reroll.

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new10.04 KB

Here I have added re-roll of patch #6.

tr’s picture

Status: Needs review » Needs work

Needs another re-roll.

v.hilkov’s picture

StatusFileSize
new10.24 KB

Re-roll of patch #13.

sadikyalcin’s picture

#15 didn't work for me. Does anyone have a working patch? Drupal 8.9.12, Votingapi 8.x-3.0-beta2

tr’s picture

didn't work for me

What is that supposed to mean? What didn't work? What errors? Be specific.

sadikyalcin’s picture

Getting the 422 Unprocessable Entity as described by OP: "Unprocessable Entity: validation failed.\nentity_id.0.target_id: The referenced entity (node: 3) does not exist". I'm voting on a comment with the id 3 not a node.

v.hilkov’s picture

StatusFileSize
new55.74 KB

Hi @sadikyalcin

Did you changed vote entity type to whatever entity type you are voting to? This can be done at /admin/structure/vote-types
Your vote type setting form should look like this:
screenshot

sadikyalcin’s picture

Hi @V.Hilkov,

I don't see the entity type setting. I only have name, value type, description and use as a reaction checkbox.

sadikyalcin’s picture

Ignore #20. Re-patched and the setting is back now.