Jump to content

Has anyone successfully uploaded a product image (using the Commerce API) with Node.js?

Recommended Posts

Hello everyone,

I'm trying to upload a product image on my AWS backend using Node.js.

I've thoroughly read documentation on how to do this with a curl request and I have no problem doing so. (Please do not direct me to that doc.)

I am continually receive INVALID_REQUEST_ERROR - The provided file could not be read as an image. No matter what I do!

Does anyone have a working example of a Javascript POST request to commerce API?

------------------------------------------------------------------------------------------------------------

This is my current upload flow this:

1). My API receives upload file - looks something like this:

Content-Disposition: form-data; name="file"; filename="thumbnail.png"
Content-Type: image/png

�PNG....

 

2). That file is then parsed - example:

{
content: <Buffer fd 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 36266 more bytes>,
filename: 'thumbnail.png',
contentType: 'application/octet-stream',
encoding: '7bit',
fieldname: 'file'
}

3). I then form request to the commerce api - formed request example:

REQUEST PARAMS = {
method: 'POST',
headers: {
'User-Agent': 'XXXX',
Authorization: 'Bearer XXXXXXXXXXXXXXXXXXXXXXXX',
'content-type': 'multipart/form-data; boundary=--------------------------186703744268236431415530'
},
body: FormData {
_overheadLength: 154,
_valueLength: 181756,
_valuesToMeasure: [],
writable: false,
readable: true,
dataSize: 0,
maxDataSize: 2097152,
pauseStreams: true,
_released: false,
_streams: [
'----------------------------186703744268236431415530\r\n' +
'Content-Disposition: form-data; name="file"; filename="thumbnail.png"\r\n' +
'Content-Type: image/png\r\n' +
'\r\n',
'/VBORw0KGgoAAAANSUhEU

3). This is the response I receive from the Commerce API:

{
type: 'INVALID_REQUEST_ERROR',
subtype: null,
message: 'The provided file could not be read as an image.',
details: null,
contextId: 'MEPRIECOYBREYASWOOBO'
}

 

I have also tried using my API as a passthrough and just forward request body and specific headers to commerce API but receive the same error. Even when I'm forwarding a curl request that I know works!

Do I need to do something special with my parsed image's Buffer before posting to commerce API?

Is this even possible?

Thanks in advance!

Link to comment
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hi,
If you haven't received an answer yet, you can post on some FB groups
+ Squarespace Customization Resource Groups
+ Squarespace Community
+ Squarespace Entrepreneur
+ Squarespace Rockstars

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.