2

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?

simone
  • 4,667
  • 4
  • 25
  • 47

1 Answers1

0

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.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
  • 1
    thanks. You're right, I'd used an old version of the script - sorry about that. However it doesn't work with the fixed version either - ``'{"entity-type":"item","numeric-id":5}'``. Should I fix the question? – simone Feb 23 '21 at 11:04
  • @simone This is the answer for your question. If you fix it, then the error you indicated would not appear anymore (so the question would be pointless). – logi-kal Feb 23 '21 at 11:24
  • Another problem is that Property:P9876 does not exist, but this is not what causes the error message that you provided. – logi-kal Feb 23 '21 at 11:25
  • If it's difficult for you to generate a valid URL, I'd suggest you to practice with the [ApiSandbox](https://www.wikidata.org/wiki/Special:ApiSandbox). – logi-kal Feb 23 '21 at 11:31
  • 1
    I found the issue thanks to your hints. I was assuming that properties and entities would be aligned with wikidata even on the test version - big mistake. I tried with (a) an existing property and (b) an existing entity and all is good. The error message is a bit terse (not a complaint), and so is the documentation at the page you point to. That said, thanks a lot for your help (no sarcasm) – simone Feb 23 '21 at 11:35