Cloudflare KV API Bulk PUT - Error 10012 - could not unmarshal KVPair into intermediate struct

348 views Asked by At

Writing a bulk PUT to the CF Workers KV in Python and can’t figure out what’s wrong here. My JSON is valid as far as I can tell.

Here is the error I’m getting

Error

{ "result": null, "success": false, "errors": [ { "code": 10012, "message": "could not parse array of key/value objects from request body: 'could not unmarshal KVPair into intermediate struct: 'json: cannot unmarshal object into Go struct field kvPairJSON.value of type string''" } ], "messages": [] }

Payload

[{ “key”:“aals”, “value”:{ “sup”:{ “firo”:“aals”, “mean”:“aals”, “alpha”:[]}}}]

Code

response = requests.put(f"{CF_BASEURL}/bulk", headers=headers, data=json.dumps(payload))

Any ideas appreciated. I'm sure this is a SMH moment...

1

There are 1 answers

1
Brett On BEST ANSWER

That's a useless error message, sorry about that. Workers KV values are plain bytes that may be deserialized in a Worker if needed (e.g. parsed as JSON). I think what you want to do there is just,

import json
payload = [{ "key": "aals", "value": json.dumps('{"sup": {"firo": "aals", "mean": "aals", "alpha": []}}')}]

So that your value is encoded as a string. And then in your Worker you can,

let jsonValue = KV_NAMESPACE.get("aals", "json");