0

I am trying to pass a variable to curl command in Perl script. But it is failing.

But when I run the same curl command from command prompt it is working.

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.

{"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.

'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.

3
  • Hi Bobby, change your code so you store the value of the system call to the variable and print it to the error log (warn) to see the exact command you are calling. One difference i noticed is that in the working version you have added a space to the id. Maybe this is the reason? HTH George Commented Dec 15, 2015 at 7:38
  • my $var=qq('curl -D- -u username:password -X PUT --data {\"fields\":{\"priority\":{\"i ... ); warn $var; system($var); Commented Dec 15, 2015 at 7:39
  • Thanks for the response. After running with your suggestion I can see the command as: curl -D- -u username:pass -X PUT --data {"fields":{"priority":{"id":"3"}}} -H "Content-Type: application/json" -k https:website But I am getting another error as below. {"errorMessages":["Unexpected character ('f' (code 102)): was expecting double-quote to start field name\n at [Source: org.apache.catalina.connector.CoyoteInputStream@22a30637; line: 1, column: 3]"]}after curl command Thanks!! Commented Dec 15, 2015 at 7:46

1 Answer 1

1

You are placing the command inside the single quote. And perl takes it as it is inside single quote. As you are passing $id variable with the command, I'll suggest you to use double quote. 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
Sign up to request clarification or add additional context in comments.

3 Comments

After adding concatenation I ma getting below error. [{"errorMessages":["Unexpected end-of-input: expected close marker for OBJECT (from [Source:org.apache.catalina.connector.CoyoteInputStream@52ba9b51; line: 1, column: 0])\n] The command I have given is: [system('curl -D- -u username:pass-X PUT --data {\"fields\":{\"priority\":{\"id\":'.${id}.'}} -H "Content-Type: application/json" -k https:request');
@Bobby your json has 3 open bracket, and 2 closing bracket. That means your json is incorrect!
@Hassan ohh yeah. I have changed it but now getting new error. {"errorMessages":[],"errors":{"priority":"Could not find valid 'id' or 'name' in priority object."}}after curl command. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.