Hi!
I’m having some issues with a customer file (and list of files) metafield to upload through the API.
When an order is placed, I need to upload an xml file to the customer metafield that I previously created through the Shopify Admin. The metafield is of the type list of files, with file type generic.
I managed to upload the file correctly, and I indeed see it in the Shopify Admin/Content/Files section, but I can’t figure out how to correctly attach it to the metafield.
I tried 2 different approaches:
- Python API
I run a python script that, after correctly handling the session and file upload, does this:
customer = shopify.Customer.find(ids = customer_id)[0]
metafield = shopify.Metafield({
'namespace': 'custom',
'key': 'license',
'value': data['resource_url'],
'owner_id': customer_id,
'type': 'file_reference'
})
response = customer.add_metafield(metafield)
where the data[‘resource_url’] is the staged upload file location.
This simply returns metafield(None). If I try to slightly edit the metafield to add a one line text customer metafield, everything works as expected.
- REST API
I request a POST with the following code:
headers = {
'X-Shopify-Access-Token': access_token,
'Content-Type': 'application/xml'
}
json = {
'metafield': {
'namespace': 'custom',
'key': 'license',
'value': data['resource_url'],
'type': 'file_reference',
'owner_id': customer_id
},
}
response = requests.post('https://eftilo.myshopify.com/admin/api/'+str(api_version)+'/customers/'+str(customer_id)+'/metafields.json', headers=headers, json=json)
this simply returns a 400 Bad Access error.
What am I doing wrong?
Thanks!