Using Environment Variables in cURL Command - Unix

后端 未结 4 1275
面向向阳花
面向向阳花 2021-02-20 10:59

My question is very simple. I want to use environment variables in a cURL command sth similar to this:

curl -k -X POST -H \'Content-Type: application/json\' -d \         


        
4条回答
  •  無奈伤痛
    2021-02-20 11:57

    curl -k -X POST -H 'Content-Type: application/json' -d '{"username":"'$USERNAME'","password":"'$PASSWORD'"}'
    

    Here the variable are placed outside of "'" quotes and will be expanded by shell (just like in echo $USERNAME). For example assuming that USRNAME=xxx and PASSWORD=yyy the argv[7] string passed to curl is {"username":"xxx","password":"yyy"}

    And yes, this will not work when $USERNAME or $PASSWORD contain space characters.

提交回复
热议问题