简体   繁体   English

如何在Perl脚本中将变量传递给curl命令?

[英]How to pass a variable to curl command in perl script?

I am trying to pass a variable to curl command in Perl script. 我试图在Perl脚本中将变量传递给curl命令。 But it is failing. 但这是失败的。

But when I run the same curl command from command prompt it is working. 但是,当我从命令提示符运行相同的curl命令时,它正在工作。

my $id=3;

system('curl -D- -u username:password -X PUT --data {\"fields\":{\"priority\":{\"id\":\"${id}\"}}} -H "Content-Type: application/json" -k https:request);

When I execute above Perl script I am getting error as below. 当我在Perl脚本上执行时,出现如下错误。

{"errorMessages":[],"errors":{"priority":"The priority selected is invalid."}}after curl command 

When I run the above command from command prompt by replacing id with value it is passing. 当我在命令提示符处通过将id替换为值来运行上述命令时,它正在传递。

'curl -D- -u username:password -X PUT --data {\"fields\":{\"priority\":{\"id\":\"3 \"}}} -H "Content-Type: application/json" -k https:request

Please help me here and let me know what is wrong in my code. 请在这里帮助我,让我知道我的代码出了什么问题。

You are placing the command inside the single quote. 您将命令放在单引号内。 And perl takes it as it is inside single quote. Perl将其视为单引号内的内容。 As you are passing $id variable with the command, I'll suggest you to use double quote. 当您通过命令传递$id变量时,建议您使用双引号。 For example 例如

system("curl ... $id... -k https:request");
       ^ double quote                   ^

Or you can do it like this way by using concatenation. 或者,您可以通过使用串联来做到这一点。

system('curl -D- -u ...' . $id . '... -k https:request');
                          ------ concatenation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM