如何在curl PUT请求的正文中使用环境变量? [英] How can I use environment variables in body of a curl PUT request?

查看:246
本文介绍了如何在curl PUT请求的正文中使用环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行一个卷曲请求,该请求在主体中使用环境变量:

I want to do a curl request which uses environment variables in the body:

curl -XPUT http://${HOST}/create -d'{"user":"${USER}"}'

在此请求中,${HOST}被正确替换为环境变量,但${USER}未被正确替换.我该如何替换${USER}?

In this request, ${HOST} is correctly replaced by the environment variable, but ${USER} is not. How can I replace ${USER} as well?

推荐答案

Shell参数扩展不会在单引号内进行.您可以关闭单引号并开始新的双引号以进行扩展:

Shell parameter expansion doesn't take place within single quotes. You can close the single quotes and start new double-quotes for the expansion:

curl -XPUT http://${HOST}/create -d'{"user":"'"${USER}"'"}'

或者您也可以使用双引号:

Or you could use double-quotes instead:

curl -XPUT http://${HOST}/create -d"{\"user\":\"${USER}\"}"

在两种情况下,您都必须能够相信$USER不包含"-您可能要先对其进行消毒:

In both cases, you need to be able to trust that $USER doesn't contain " - you might want to sanitize it first:

# This is a Bash extension to POSIX
USER=${USER//\"}

这篇关于如何在curl PUT请求的正文中使用环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆