Update photos by API

sdohmensdohmen Posts: 12Member
Hello,

Since several months we are using the API to create and update new products. With most fields we have no problems and i can fill them just fine. Since a couple days we decided to also use the API for adding pictures to the products but i keep failing at it.

I tried the following curl url (curl -X "POST" https://api.webshopapp.com/nl/products/$productid/images.json -u key::secret -d productImage[attachment]="$IMAGE_BASE64" -d productImage[filename]="$picname") but it did not work because of an error (curl: Argument list too long).

Then i tried to echo the base64 encode like below but either lines do not work and gave me errors:

echo { "productImage": { "attachment": "$IMAGE_BASE64", "filename": "$picname" } } | curl -g -X POST https://api.webshopapp.com/nl/products/$productid/images.json -u key:secret --data @-

Error: {"error":{"code":400,"method":"CREATE","request":"\/nl\/products\/7649\/images.json","message":"Invalid data input."}}

(echo -n '{ "productImage": {"attachment": "'; base64 $PAD/$picname; echo '"}}') | curl -g -X POST -u key:secret -d productImage[filename]="$picname" -d @- ; https://api.webshopapp.com/nl/products/$productid/images.json

Error: {"error":{"code":400,"method":"CREATE","request":"\/nl\/products\/7649\/images.json","message":"Invalid file attachment given."}}

Does anyone have an idea how i can do this so i can update the images with the API also?

Best regards,
Sven




4 comments

  • jamesratcliffejamesratcliffe Posts: 160Administrator, Lightspeed Staff moderator

    @sdohmen The problem is just with the string quoting. This example should work:

    curl -X POST \
        https://api.webshopapp.com/en/products/75587420/images.json \
        -u "$KEY:$SECRET" \
        -d '{"productImage": {"attachment": "'$IMAGE_BASE64'", "filename": "'$PICNAME'"}}'

    I think that piping the echo command into curl is adding extra complexity. If you want to simplify the curl command, you could define the data payload first, then pass it to curl as a variable:

    DATA='{"productImage": {"attachment": "'${IMAGE_BASE64}'", "filename": "'${PICNAME}'"}}'
    
    curl -X POST \
        https://api.webshopapp.com/en/products/75587420/images.json \
        -u "$KEY:$SECRET" \
        -d "$DATA"

    Here's another option for quoting the DATA string:

    DATA="{\"productImage\": {\"attachment\": \"$IMAGE_BASE64\", \"filename\": \"$PICNAME\"}}"
    James Ratcliffe
    API Integrations Specialist
    Lightspeed HQ
  • sdohmensdohmen Posts: 12Member
    Hello James,

    Thank you for trying to help but i am afraid neither seems to work. The first one gives me the error: curl: Argument list too long. This is understandable since the whole picture/variable is too big (around 7MB).

    I tried the second and last one and i got the errors: {"error":{"code":400,"method":"CREATE","request":"\/nl\/products\/76550225\/images.json","message":"Invalid data input."}}
    and
    {"error":{"code":400,"method":"CREATE","request":"\/nl\/products\/76550225\/images.json","message":"Invalid file attachment given."}}

    I also tried to echo it first but this did not change the result. Is there by any change a limit on the picture allowed to upload? Or do you perhaps have another idea how i can send the data by curl.

    Best regards,
    Sven


  • jamesratcliffejamesratcliffe Posts: 160Administrator, Lightspeed Staff moderator
    edited August 2018
    @sdohmen "Argument list too long" means that the base64 string is too long use directly in the curl command.

    This script worked for me with a large image:
    #!/bin/bash
    
    IMAGE_BASE64=$(base64 image.jpg)
    PICNAME="image.jpg"
    DATA='{"productImage": {"attachment": "'${IMAGE_BASE64}'", "filename": "'${PICNAME}'"}}'
    
    echo $DATA | curl -X POST \
       "https://api.webshopapp.com/en/products/XX/images.json" \
        -u "key:secret" \
        -d @-
    James Ratcliffe
    API Integrations Specialist
    Lightspeed HQ
  • sdohmensdohmen Posts: 12Member
    Hello James,

    I got it to work with your example above. It seems the encoding what i used was wrong as i did base64 -o picture.jpg instead of just base64 picture.jpg.

    Thank you for the help.

    Best regards,
    Sven
Sign In or Register to comment.