Why isn't this JSON post request working?

Highlighted
New Member
3 0 0

I'm trying to create a metafield on my product.

 

body: {"key":"color_images","namespace":"plum_berry","value":"test","value_type":"string"}

 

With response:
[0] json { errors: { metafield: 'Required parameter missing or invalid' } }


What am I doing wrong? Here's the entire code:

 

	const url = base_url + `admin/api/2020-01/products/${owner_id}/metafields.json`;
	console.log('url: ', url);
	try {
		const params = {
			key,
			namespace,
			value: value,
			value_type
		};
		const body = JSON.stringify(params);
		console.log('body: ', body);
		const mf = await fetch(url, {
			method: 'POST',
			body,
			headers
		});
		let json = await mf.json();
		console.log('json', json);
		return mf;
	} catch (e) {
		throw e;
	}
0 Likes
Highlighted
New Member
3 0 0

Nevermind, i'm an idiot and didn't wrap the params in a metafield key:

 

const params = {
metafield: { key, namespace, value: JSON.stringify(value), value_type }
};
const body = JSON.stringify(params);
0 Likes