Search code examples
azure-devopsazure-cli

Azure CLI: Upload file using REST / multipart/form-data


I have a custom Azure service that offers me following REST endpoints:

  • GET .../files (gets a json list of all currently available files)
  • GET .../files/fileId (gets the content of one specific file)
  • POST .../files (allows to upload a new file) This endpoint requires the file to be uploaded with the header content-type: multipart/form-data.

Note that this does not use a blob storage or storage account. It's a custom implementation specific to our needs.

Now I want to upload new files from our Azure DevOps build pipeline. I've created a Command line task and was able to successfully execute these commands:

  • az login using service principal information
  • az rest executing the GET request to list the available files.

Where I'm struggeling is to execute the POST request as it seems the command az rest does not offer to upload a file using multipart/form-data, or maybe I just didn't find out how.

Any suggestions on how I can achieve this?

As a workaround, I've also tried to use a curl command to upload the file. It would work if I manually paste the Baerer token into the command, but I don't see a way how I could access the token from az login in the bash script and use it in the curl command.


Solution

  • As you mentioned, az rest doesn't natively support multipart/form-data. Also, you're correct that using curl is the right way to go.

    You should be able to fetch your token from az login by using:

    token=$(az account get-access-token --query accessToken --output tsv)
    

    And then, simply include the token variable in your curl bash command:

    curl -X POST "https://url/files" -H "Authorization: Bearer $token" -F "file=@path/to/your/file"