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
  • Views 555
  • Created
  • Last Reply

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.