KV API giving FormData errors

I'm trying to write to KV, following the API docs here: https://developers.cloudflare.com/api/operations/workers-kv-namespace-write-key-value-pair-with-metadata However I continue to get this response
{
result: null,
success: false,
errors: [
{
code: 10047,
message: "can not parse value and metadata from multipart request body: 'error getting part of multipart request, after reading 0 parts: 'multipart: NextPart: EOF''"
}
],
messages: []
}
{
result: null,
success: false,
errors: [
{
code: 10047,
message: "can not parse value and metadata from multipart request body: 'error getting part of multipart request, after reading 0 parts: 'multipart: NextPart: EOF''"
}
],
messages: []
}
Here's my code with some hardcoded values for testing:
export async function kvSet(key: string, value: any, opts: { metadata?: Record<string, string> }) {
const form = new FormData();
form.append("metadata", "");
form.append("value", "test");


const options = {
method: 'PUT',
headers: {
'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001',
Authorization: `Bearer ${env.KV_AUTH_TOKEN}`
},
};
options.body = form
console.log(options)



const response = await fetch(`https://api.cloudflare.com/client/v4/accounts/${env.KV_ACCOUNT_ID}/storage/kv/namespaces/${env.KV_NAMESPACE_ID}/values/${key}`, options)
const result = await response.json()
console.log("set result", result)
return result.result

}
export async function kvSet(key: string, value: any, opts: { metadata?: Record<string, string> }) {
const form = new FormData();
form.append("metadata", "");
form.append("value", "test");


const options = {
method: 'PUT',
headers: {
'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001',
Authorization: `Bearer ${env.KV_AUTH_TOKEN}`
},
};
options.body = form
console.log(options)



const response = await fetch(`https://api.cloudflare.com/client/v4/accounts/${env.KV_ACCOUNT_ID}/storage/kv/namespaces/${env.KV_NAMESPACE_ID}/values/${key}`, options)
const result = await response.json()
console.log("set result", result)
return result.result

}
Cloudflare API Documentation
Interact with Cloudflare's products and services via the Cloudflare API
3 Replies
Cyb3r-Jak3
Cyb3r-Jak311mo ago
Is this from a worker?
fasm
fasmOP11mo ago
No it's not. I solved this by removing the 'Content-Type' header. Not sure why it's there in the docs? also the docs don't say how to add expirations?
Cyb3r-Jak3
Cyb3r-Jak311mo ago
I think there’s a special way to get the boundary part. For the expiration it should be be other values of the form for it
Want results from more Discord servers?
Add your server