Monday, February 21, 2022

[SOLVED] How to use a variable in curl command header section

Issue

I am trying to use a variable in the header of a curl command, can anyone please help in finding the solution?

curl --location \
  --request POST https://abc/bcd \
  --header 'Content-Type: application/json' \
  --header 'id: $empid' \
  --data-raw '{
                    "clientName": "name",
                    "role": "dev",
                    "group": "groupid"
                    }'

Here I am trying to get the empid value in the header section, but I am not getting the value instead getting the same variable $empid.

Thanks in advance.


Solution

Rewrite you script as follow:

curl --location \
  --request POST https://abc/bcd \
  --header "Content-Type: application/json" \
  --header "id: $empid" \
  --data-raw "{
                    \"clientName\": \"name\",
                    \"role\": \"dev\",
                    \"group\": \"groupid\"
                    }"

If you need variable sostitution you have to use double quotes instead of single quotes.



Answered By - Antonio Petricca
Answer Checked By - Marilyn (WPSolving Volunteer)