0

I have a method where I'm executing a curl command and it returns the result. I would like to pass arguments to this method but it isn't working.

commande_mine() {
    local MY_OUTPUT=$(curl -X POST \
  http://localhost:5000/myapp \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
        "filepath": "$1"
    }')
    echo $MY_OUTPUT
}

for f in "/Users/anthony/my files"/*
do 
    commande_mine $f >> test.txt
    break # break first time until everything works as expected
done

How can I use the $f parameter passed into the function inside the curl command?

Anthony
  • 33,838
  • 42
  • 169
  • 278

1 Answers1

1

You can use:

commande_mine() {
  local MY_OUTPUT=$(curl -X POST \
  http://localhost:5000/myapp \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
        "filepath": "'"$1"'"
    }')
    echo "$MY_OUTPUT"
}

and call it as:

for f in "/Users/anthony/my files"/*
do 
    commande_mine "$f"
    break
done > test.txt
anubhava
  • 761,203
  • 64
  • 569
  • 643