Search code examples
python-requestsmediawiki-apiwikibase

Submitting time data to wikibase with `wbcreateclaim` results in "invald-snak" error


I am currently trying to populate a wikidata instance via POST requests. For this purpose I use the requests library in Python together with the MediaWiki API.

So far I managed to create claims with different datatypes (like String, Quantity, Wikidata items, Media ...). The general scheme I use is this (with different value strings for each datatype):

import requests
session = requests.Session()
# authenticate and obtain a csrf_token
parameters = {
    'action':   'wbcreateclaim',
    'format':   'json',
    'entity':   'Q1234',
    'snaktype': 'value',
    'property': 'P12',
    'value':    '{"time": "+2022-02-19T00:00:00Z", "timezone": 0, "precision": 11, "calendarmodel": "http://www.wikidata.org/entity/Q1985727"}',
    'token':     csrf_token,
    'bot':       1,
}
        
r = session.post(api_url, data=parameters)
print(r.json())

Every attempt to insert data of type time leads to an invalid-snak error (info: "invalid snak data.").

The following changes did not solve the problem:

  • submitting the value string as dictionary value (without the single quotes),
  • putting the numeric values into (double) quotes,
  • using a local item for the calendarmodel ("http://localhost:8181/entity/Q73"),
  • adding before and after keys in the dictionary,
  • omitting timezone, precision, calendarmodel and combinations thereof,
  • formatting the time string as 2022-02-19,
  • submitting the request with administrator rights (though the error message does not suggest a problem with insufficient user rights).

Do you know, what I'm doing wrong here? What must the value field look like?

I am aware of the fact that special libraries or interfaces for exist for these tasks. But I do want to use the Wikidata API directly with the requests library in Python.

Thank you very much for your help!

Installed software versions:

  • MediaWiki: 1.36.3
  • PHP: 7.4.27
  • MariaDB 10.3.32-MariaDB-1:10.3.32+maria~focal
  • ICU 67.1

Solution

  • It works if the value string is generated from the dictionary via json.dumps().