Item Image Upload

reportsreports Posts: 8Member

I am trying to upload images and am struggling to understand the correct format.


I am successful in uploading an image using Postman but not using Microsoft Flow (which is what I want to use).


What is the POST endpoint https://api.merchantos.com/API/Account/{account#}/Item/{itemID}/Image.json looking for?

I am posting a HTTP request with the header:

{

 "Content-Type": "application/x-www-form-urlencoded",

 "Accept": "application/json"

}

and body:

{

 "image": "{base64 encoded image data}",

 "filename": "test.jpg",

  "data": {

  "description": "Test Image",

  "ordering": 1

 }}


but it rejects with a 500 error

"message": "Missing required parameter - file".

but the file is the base64 encoded image data in the "image" field above??


Any pointers?!


Thanks!

4 comments

  • gregaricangregarican Posts: 228Member 

    About a year ago I imported a couple thousand products into LS Retail, and then added primary images for them. Looking back at my sample request/response captures, I called this endpoint:

    https://api.merchantos.com/API/Account/{AccountId}/Image 


    I used multi-part form data, where the first part consisted of the filename, the optional description, and the itemID. The second part consisted of a MIME block that passed along the image/jpeg raw source. This all worked fine IIRC.

    Here's an example:

    POST https://api.merchantos.com/API/Account/{AccountId}/Image HTTP/1.1

    Authorization: Bearer {MyAuthToken}

    Accept: application/xml

    Content-Type: multipart/form-data; boundary=---------------------------8d5776ed3b81995

    Host: api.merchantos.com

    Content-Length: 25759

    Expect: 100-continue



    -----------------------------8d5776ed3b81995

    Content-Disposition: form-data; name="data"


    <?xml version="1.0"?>

    <Image xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

     <filename>39b3c95943780c38a02cf9288a93687f.jpeg</filename>

     <description />

     <itemID>227</itemID>

    </Image>

    -----------------------------8d5776ed3b81995

    Content-Disposition: form-data; name="image"; filename="39b3c95943780c38a02cf9288a93687f.jpeg"

    Content-Type: image/jpeg


       JFIF  ` `   C     

    ...

    Have you read the API docs to see other examples?

  • reportsreports Posts: 8Member

    Thanks so much for responding Gregarican!


    No that isn't working either. I'm using the instructions from https://developers.lightspeedhq.com/retail/endpoints/Item-Image/

    and I'm using the endpoint /API/Account/{Account ID}/Item/{Item ID}/Image.json.

    If I try the multipart post it rejects with 400 "JSON Input was invalid. Error Message: Syntax error".


    It seems Lightspeed is looking for JSON when I am trying to send multipart. If I send as JSON it can't see the base64 file data!!


    Any other advice??

  • reportsreports Posts: 8Member

    I FINALLY solved it. I don't know what the "application/octet-stream" data is though?

    Anyway, here is the body of the request needed using Microsoft Flow HTTP action:

    {

     "$content-type": "multipart/form-data; boundary=--------------------------084433617896078964587340",

     "$multipart": [

      {

       "headers": {

        "Content-Disposition": "form-data; name=\"image\"; filename=\"FILE NAME HERE\"",

        "Content-Type": "image/jpeg"

       },

       "body": {

        "$content-type": "image/jpeg",

        "$content": "BASE64 ENCODED IMAGE HERE"

       }

      },

      {

       "headers": {

        "Content-Disposition": "form-data; name=\"data\""

       },

       "body": {

        "$content-type": "application/octet-stream",

        "$content": "ewogICAgImRlc2NyaXB0aW9uIjogIlRlc3QgSW1hZ2UiLAogICAgIm9yZGVyaW5nIjogMQp9"

       }

      }

     ]

    }

  • gregaricangregarican Posts: 228Member 

    Ahhh, glad you figured it out! I sent my request as XML, so it was a different content type underneath that I suppose...

Sign In or Register to comment.