0

From perl i'm trying to call a servicenow rest api to update some data attributes.

I'm using curl command to achieve this and for some reasons i can't use any of the perl modules available.

I'm able to achieve this successfully without any special characters in the value field of the json.

Following is the code used for formatting the cmd:

my $comments = "b'c";
my $cmd = `curl \"$url\" -i -s --insecure --user test:test --request PUT --header "Accept:application/json" --header "Content-Type:application/json"  --data '{\"comments\":\"$comments\"}'`;

If the above value is "bc" i'm able to get the data, but if i give "b'c" the i'm getting following errors:

sh: -c: line 0: unexpected EOF while looking for matching `"'

sh: -c: line 1: syntax error: unexpected end of file

Even i tried the following code:

my $cmd = system "curl https://test.service-now.com/api/now/table/incident/code?sysparm_display_value=true -i -s --insecure --request PUT --header \"Accept:application/json\" --header \"Content-Type:application/json\"  --data '{\"comments\":\"bc\"}' --user test:test";

If a string with single quote b'c is given I'm getting the same error.

Could somebody please tell me how to handle single quote inside double quoted string?

3
  • If you add four spaces at the start of a line, then StackOverflow will format that line as code. I've just edited your question to do that. Please consider doing it yourself in future.
    – Dave Cross
    Dec 18, 2018 at 12:42
  • If you run the curl command directly from the command line (bash or so), is it working ?
    – GMB
    Dec 18, 2018 at 13:22
  • yes, it is working
    – Varun C.h
    Dec 18, 2018 at 16:17

1 Answer 1

1

I can get this working with

my $comments = "b\"'\"c";

The string that gets passed to the shell is then

--data '{"comments":"b'"'"'c"}'

which is three separate tokens concatenated together:

'{"comments":"b'       resolves to    {"comments":"b
"'"                    resolves to    '
'c"}'                  resolves to    c"}

Also see String::ShellQuote, which is a godsend for problems like this.

use String::ShellQuote;
$comments = "b'c";
@cmd = ("curl", $URL, "-i", "-s", "--insecure", "--request",
        "PUT", "--header", "Accept:applicatin/json", "--header",
        "Content-Type:application/json",
        "--data", qq[{"comments":$comments}], "--user", "test:test");
$cmd = shell_quote(@cmd);
print $cmd;

Gives you:

curl 'https://test.service-now.com/api/now/table/incident/code?sysparm_display_value=true' 
    -i -s --insecure --request PUT --header 
    Accept:application/json --header Content-Type:application/json 
    --data '{"comments":"b'\''c"}' --user test:test

which would also satisfy the shell's syntax checker.

1
  • Thanks for the help. It worked. I tried with slight modifications my @cmd = ("curl", $url, "-i", "-s", "--insecure", "--request", "PUT", "--header", "Accept:application/json", "--header", "Content-Type:application/json", "--data", qq[{"comments":\"$comments\"}], "--user", "test:test");
    – Varun C.h
    Dec 19, 2018 at 6:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.