1

I am looking to upload a multipart/form-data file upload. I working on a circleci job to auto deploy a couple files of a specific file type all in one go.

This solution works but it is making a seperate curl call for each file.

cd ~/project;find . -name "*.txt*" -type f -exec curl -k -H "Accept: application/json" -H "Authorization: Bearer $bearertoken" -F "deployment-name=${CIRCLE_SHA1:0:7}" -F "deployment-source=circleci" -F "enable-duplicate-filtering=false" -F "deploy-changed-only=true" -F "{}=@{}" http:www.blah.com  \;

I am wondering if there is a way that I can build a file list of all the files in the directory and then use that variable in a single curl upload.

2
  • 1
    It depends on whether the script you're sending to allows multiple uploads. Commented Mar 3, 2021 at 22:49
  • Yes, it supports multiple files in a single update. For example it would work like this -F "./file1=@./file1.txt" -F "./file2=@./file2.txt Commented Mar 4, 2021 at 14:53

1 Answer 1

4

Use a loop to create an array of alternating -F and filename=@filename. Then substitute the array into the curl command.

files=()
while read -r filename; do
    files+=(-F "$filename=@$filename")
done < <(find . -name '*.txt' -type f)

curl -k -H "Accept: application/json" -H "Authorization: Bearer $bearertoken" "${files[@]}" -F "deployment-name=${CIRCLE_SHA1:0:7}" -F "deployment-source=circleci" -F "enable-duplicate-filtering=false" -F "deploy-changed-only=true" "${files[@]}" http:www.blah.com
Sign up to request clarification or add additional context in comments.

4 Comments

Surely there's an extra $ in "${$files[@]}"
This is exactly what I was looking to do. Thank you. made some slight modifications to my scripts.
this script doesn't work
@b26 There was a typo that Charles Duffy noticed but I never fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.