Search code examples
wikidatawikidata-api

Why "Invalid snak data" on updating wikibase?


I am trying to learn how to update values on Wikidata using the API. Login and csrf cookies work ok, but when I try to update a value I get an invalid-snak error.

The request body looks like this:

POST /w/api.php HTTP/1.1
Accept-Encoding: gzip
Content-Length: 178
User-Agent: Mojolicious (Perl)
Host: test.wikidata.org
Content-Type: application/x-www-form-urlencoded
Cookie: [omitted]

action=wbcreateclaim&bot=1&entity=Q3345&format=json&property=P9876&snaktype=value&token=[omitted]&value=%7B%22entity-type%22%3A%22Q1917%22%7D

and the response is:

{
  "error": {
    "code": "invalid-snak",
    "info": "Invalid snak data.",
    "messages": [
      {
        "name": "wikibase-api-invalid-snak",
        "parameters": [],
        "html": {
          "*": "Invalid snak data."
        }
      }
    ],
    "*": "See https://test.wikidata.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce> for notice of API deprecations and breaking changes."
  },
  "servedby": "mw1386"
}

I've tried various ways to succeed with an update by changing the value - no results. The only update I succeeded with is one with snaktype=novalue - which would confirm that the issue is with the snak vaue alone.

So the question is, what's the right way to make an update to a snakvalue?


Solution

  • The problem is that you are stating value={"entity-type":"Q1917"}, but Q1917 is not an entity-type!

    You should instead state value={"entity-type":"item","numeric-id":1917}.

    For deepen the topic, see the Wikidata API's documentation.