Suhas
Suhas

Reputation: 17

echo contents of file into a curl command

I am trying to run a curl command something like below:

curl -X POST "https://$CLUSTER/api/internal/vmware/vm/snapshot/$snapshot_id/restore_files" -H "accept: application/json" -H "authorization: "$authType" "$hash_password"" -H "Content-Type: application/json" -d "{\"destObjectId\": \"$vm_id\", \"restoreConfig\": [ echo $(cat suhas_file.txt) ], \"shouldUseAgent\": true, \"shouldRestoreXAttrs\": true}" -k -s

I get Request Malformed

Due to the echo $(cat suhas_file.txt)

If I run this echo command manually I get:

echo $(cat suhas_file.txt)
{\"path\": \"C:\\\\bootmgr\"},{\"path\": \"C:\\\\Program Files\\\\desktop.ini\"}

If I paste this manually instead of echo in the above curl like

curl -X POST "https://$CLUSTER/api/internal/vmware/vm/snapshot/$snapshot_id/restore_files" -H "accept: application/json" -H "authorization: "$authType" "$hash_password"" -H "Content-Type: application/json" -d "{\"destObjectId\": \"$vm_id\", \"restoreConfig\": [ {\"path\": \"C:\\\\bootmgr\"},{\"path\": \"C:\\\\Program Files\\\\desktop.ini\"} ], \"shouldUseAgent\": true, \"shouldRestoreXAttrs\": true}" -k -s

It works.

I tried

value=`cat suhas_file.txt`

And then echo $value inside the curl and I still get malformed request.

How can i basically echo the contents of this file that we see above within the [] of curl

Upvotes: 0

Views: 1140

Answers (2)

Léa Gris
Léa Gris

Reputation: 19675

To safely compose a dynamic JSON data-set from a shell script, a JSON-aware parser like jq will ensure proper formatting.

Example:

#!/usr/bin/env sh

authType='authTypeBasic'
hash_password='samplePassword'
snapshot_id='snapshot42'
vm_id='Marvin'

data="$(
  jq \
    --null-input \
    --compact-output \
    --arg VMID "$vm_id" \
    --rawfile restoreConfig suhas_file.txt \
    '{
      "destObjectId": $VMID,
      "restoreConfig": [ $restoreConfig | split("\n") ],
      "shouldUseAgent": true,
      "shouldRestoreXAttrs": true
    }'
  )"
curl \
  --request 'POST' \
  --header "Accept: application/json" \
  --header "Authorization: $authType $hash_password" \
  --header "Content-Type: application/json" \
  --data "$data" \
  --insecure \
  --silent \
  --url "https://$CLUSTER/api/internal/vmware/vm/snapshot/$snapshot_id/restore_files"

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 247102

Try:

printf -v data '
    {
        "destObjectId": "%s",
        "restoreConfig": [%s],
        "shouldUseAgent": true,
        "shouldRestoreXAttrs": true
    }
' "$vm_id" "$(< suhas_file.txt)"

curl -X POST \
    -H "accept: application/json" \
    -H "authorization: $authType $hash_password"" \
    -H "Content-Type: application/json" \
    -d "$data" \
    -k -s \
    "https://$CLUSTER/api/internal/vmware/vm/snapshot/$snapshot_id/restore_files"

Added line continuations for readability.

In the suhas_file.txt file, do not escape the double quotes -- it should be plain JSON data.

$(< filename) is a bash construct, equivalent to $(cat filename). See 3.5.4 Command Substitution in the manual

Upvotes: 1

Related Questions