Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Robin Macharg 3 posts 73 karma points
    Nov 06, 2017 @ 13:59
    Robin Macharg

    Wanted: Curl example of using ReST API to upload an image

    Hi. First time poster here; experienced developer but new to Umbraco. Has anyone successfully uploaded a binary file (e.g. image) to the REST API using cURL in a Bash script? I've got as far with the REST API as authenticating and creating the media item. When I try and PUT to the 'upload' resource I'm running into problems. Looking at the Chrome dev tool for a 'normal' webUI upload request I can see that there's a multipart/form-data body with some additional fields but Chrome hides any actual file contents. I know that I have to honour the CRLF convention, and that Bash/echo is funny about quoting the \r\n. Beyond that the 'Unexpected end of MIME multipart stream.' error I'm seeing is not useful. I've read the source of the relevant REST MediaController file and can see what is supposed to be happening in the UploadFile() method but am obviously missing something. If anyone's got an actual example of a small shell script that contains the correct Bash invocation that would be really useful. Thanks.

    Update: I've successfully uploaded a binary file via the backoffice API. I'd still prefer to drive this via REST, rather than mimicking a browser (lifting cookies into HTTP headers gets messy), so suggestions still gratefully received. Happy to post the cURL commands that have worked for me if anyone's interested.

  • Robin Macharg 3 posts 73 karma points
    Jan 16, 2018 @ 09:20
    Robin Macharg

    I'm not all the way there yet, but I'll start to answer this myself. I'm on a Mac, so this is Bash command-line specific, but easily translated to Windows CMD. Given an Umbraco instance configured for ReST access (use the Github repo, not the NuGet package since it's actually under active development, AFAICT), some handy cURL commands:

    # Authenicate
    echo ==== LOGIN ====
    JSON_OUT=$( curl -s -X "POST" "$TOKEN_URI" \
         -H 'Cache-Control: no-cache' \
         -H 'Content-Type: application/x-www-form-urlencoded' \
         -H 'Accept: application/json' \
         --data-urlencode "grant_type=password" \
         --data-urlencode "username=$USERNAME" \
         --data-urlencode "password=$PASSWORD" )
    echo $JSON_OUT | jq .
    ACCESS_TOKEN=$( echo $JSON_OUT | jq -r .access_token )
    echo ==== CREATE MEDIA FOLDER ====
        -b cookies.txt -c cookies.txt \
        -H "Authorization: Bearer $ACCESS_TOKEN" \
        -H 'Content-Type: application/json; charset=utf-8' \
        -d $'{
               "ParentId": "-1",
               "contentTypeAlias": "Folder",
               "name": "MyMediaFolder",
               "currentFolder": "-1"
             }' )
    echo $JSON_OUT | jq .
    FOLDER_ID=$( echo $JSON_OUT | jq .id )
    echo $FOLDER_ID
    # Create Media
    echo ==== CREATE MEDIA ====
    JSON_OUT=$( curl -s -X "POST" "$CREATE_MEDIA_URI" \
        -b cookies.txt -c cookies.txt \
        -H "Authorization: Bearer $ACCESS_TOKEN" \
        -H 'Content-Type: application/json; charset=utf-8' \
        -d "{
               'ParentId': '$FOLDER_ID',
               'contentTypeAlias': 'Image',
               'name': 'Placeholder',
               'currentFolder': '-1',
               'properties' : {
                   'tags' : 'tag1, tag2, tag three'
             }" )
     echo $JSON_OUT | jq .
     UPLOAD_URI=$( echo $ROOT$( echo $JSON_OUT | jq -r ._links.upload.href ) | cut -d'{' -f1 )

    ... And there I'm drawing a blank. Worth noting I've enabled a Tags field on the Image media type and the placeholder image/media resource is being tagged correctly. I've got a command that in theory should upload a file, and seems to work (in that it returns "null" and does not error). But I'm not seeing the file either on disk on in the media section.

    # Upload Media
    echo ==== UPLOAD MEDIA ====
    echo -en $'----BOUNDARY\r\nContent-Disposition: form-data; name="file"; filename="myImage.png"\r\nContent-Type: image/png\r\n\r\n' > complete
    cat $FILENAME >> complete
    echo -en $'\r\n----BOUNDARY--\r\n' >> complete
    JSON_OUT=$( curl -s -X "PUT" "$UPLOAD_URI" \
        -b cookies.txt -c cookies.txt \
        -H "Authorization: Bearer $ACCESS_TOKEN" \
        -H "Content-Type: multipart/form-data; charset=utf-8; boundary=--BOUNDARY" \
        --data-binary '@complete' )
    echo $JSON_OUT

    Any suggestions on what to investigate next would be gratefully received.

  • Laura Liu 1 post 71 karma points
    Apr 13, 2018 @ 21:35
    Laura Liu

    Hi Robin,

    I am interested in seeing your cURL call. Can you please post it here and show me?

    Thanks! Laura

Please Sign in or register to post replies

Write your reply to: