I'm trying to use the CloudKit API to create records in the public database from my server, using a server-to-server key. I'm using the following shell script to generate the curl command. When I run it, the response from Apple just says there was an internal error.
{ "uuid" : "a6415feb-168b-4615-9577-10c5168d7d7c", "serverErrorCode" : "INTERNAL_ERROR" }
This is the script I'm using:
#!/bin/sh
subpath=/database/1/iCloud.com.mycompany.myapp/development/public/records/modify
date=`date -u +"%Y-%m-%dT%H:%M:%SZ"`
body='
{
"operations": [
{
"operationType": "forceReplace",
"record": {
"recordType": "Drawing",
"fields": {
"date": "2021-01-09T12:00:00Z",
"numbers": [14, 26, 38, 45, 46, 13],
"type": 1
}
},
"recordName": "powerball20210109"
}
],
"atomic": true
}
'
encoded=`echo $body | base64`
signature="$date:$encoded:$subpath"
curl -X POST https://api.apple-cloudkit.com$subpath \
-H 'Content-Type: application/json' \
-H 'X-Apple-Cloudkit-Request-KeyID: myKeyHere' \
-H "X-Apple-CloudKit-Request-ISO8601Date: $date" \
-H "X-Apple-CloudKit-Request-SignatureV1: $signature" \
-d "$body"
The CloudKit schema shows Drawing as a custom type with three properties:
I can see a couple of issues. All fields need to have an object with a value
property. Also, dates are saved to CloudKit as an integer in the format of milliseconds since Jan 1, 1970.
I've never tried saving an array of integers like that, but it looks right.
Try this:
"fields":{
"date": { "value" : 1610193600000 },
"numbers": { "value" : [14, 26, 38, 45, 46, 13] },
"type": { "value" : 1 }
}