ksho Posted August 16, 2021 Share Posted August 16, 2021 (edited) Hi! I'm trying to programmatically upload photos to my products (https://developers.squarespace.com/commerce-apis/upload-product-image). CURL Requests via Insomnia will work, but I cannot for the life of me get uploads via Python working. My code (generated from a working Insomnia request): url = "https://api.squarespace.com/1.0/commerce/products/MY_PRODUCT_ID/images" payload = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"file\"; filename=\"MY_FILE_NAME.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n-----011000010111000001101001--\r\n" headers = { "Authorization": "Bearer MY_TOKEN", "User-Agent": "MY_USER_AGENT", "Content-Type": "multipart/form-data; boundary=---011000010111000001101001" } response = requests.post(url, data=payload, headers=headers) I keep hitting this error: "The provided file could not be read as an image." Has anyone done this before? What am I missing? If anyone has code examples from any language I'm happy to reverse engineer! Edited August 16, 2021 by ksho clarifying some words Link to comment
tuanphan Posted August 18, 2021 Share Posted August 18, 2021 Hi, If you haven't still solved this yet, you can post on some FB groups + Squarespace Customization Resource Groups + Squarespace Community Email me if you have need any help (free, of course.). Answer within 24 hours. Or send to forum message How to: Setup Password & Share url - Insert Custom CSS - Page Header - Upload Custom Font - Upload File - Find Block ID - Contact Customer Care Link to comment
Alexander_SQSP Posted August 20, 2021 Share Posted August 20, 2021 Hello @ksho, Thanks for posting your question! This example currently works for me, though I am not exactly sure the syntax in your example is correct: import requests url = "https://api.squarespace.com/1.0/commerce/products/<product id>/images" files=[ ('file',('<filename>',open('<path to image>','rb'),'image/jpeg')) ] headers = { 'Authorization': 'Bearer <api key>', 'User-Agent': '<your user-agent>' } response = requests.request("POST", url, headers=headers, files=files) print(response.text) Also, check that your images meets the platform requirements Let me know if this resolves the issue! creedon 1 Build together: Together, we make up a community of Squarespace professionals. Lean on your fellow community members for support and welcome diverse perspectives. Work in progress: Trust that we are all working for the collective good of Circle members and their clients. Keep it light mode: Balance productivity with joyful creativity and curiosity. Link to comment
CKAKUMANU7 Posted June 15 Share Posted June 15 What fixed it for me was NOT to add the below in the headers "Content-Type": "multipart/form-data;" Praise 1 Link to comment
Praise Posted August 4 Share Posted August 4 This worked for me as well On 6/14/2023 at 9:23 PM, CKAKUMANU7 said: What fixed it for me was NOT to add the below in the headers "Content-Type": "multipart/form-data;" creedon 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment