Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
my $jsonparams = qq({ "fields": { "project": {"key": "MCTEST"},"summary": "$summary", "description": "$description", "issuetype": {"name": "Incident"},"customfield_1 +3060": [{"value": "$class1"}, {"value": "$class2"}, {"value": "$class +3"}]}});
The $description variable can be quite long, with all sorts of white spaces, line/carriage returns, urls with weird non-ascii information, etc. Basically, I have no idea what it will contain. But I want to basically pass it *all* within json to use with Jira's rest API. I'd like a way to escape everything in the variable so that it can plug into the JSON I have above.
Or... if there is a better way to go about it (almost guaranteed I bet), I would love any advice.
And yes... I know this script is sad.. and frankensteinesque.
Here is what I have so far (more or less):
#! /usr/bin/perl -w use REST::Client; use JSON; use Data::Dumper; use MIME::Base64; use URI::Escape; use strict; #print Dumper \@ARGV; my $data = ''; # $data is a temp variable to join all commandline data (@ARGV) -- so +that it can be split by special delimitter I invoke from command line +. # Example: /home/user/create-test-ticket.pl "$OPC_MSG.APPLICATION" + '^^_^^' "$OPC_MSG.NODE" '^^_^^' "$OPC_MSG.OBJECT" '^^_^^' "$OPC_MSG. +SEVERITY" '^^_^^' "$OPC_MSG.GROUP" '^^_^^' "$OPC_MSG.MSG_ID" '^^_^^' +"$OPC_MSG.RECEIVED" '^^_^^' "$OPC_USER" '^^_^^' "$OPC_MSG.TEXT" # # This is to work around shell splitting contents of command line vari +ables from OVO by spaces. foreach my $argnum (0 .. $#ARGV) { $data .= $ARGV[$argnum] . " "; } #print Dumper $data; my ($app, $node, $object, $severity, $group, $id, $received, $user, $m +sg_text) = split /\^\^_\^\^/, $data; # This section is for testing out multiple alert support. WIP . #my $href; # testing my @ids = split / /, $id; if ($#ids > 1) { print "Sorry -- this tool doesn's support multiple alerts yet. That wi +ll be implemented in the next version. \n"; exit; } my $summary = $severity . ' ALERT for: ' . $app . ' on : ' . $node . '( ' . $object . ' )'; chomp $summary; my $username = 'user'; my $jiraUrl = 'http://example.jira.com'; my $password = `decrypt.sh "FOOBARFTW!!"`; chomp $password; my $headers = { Content_Type => 'application/json', Accept => 'application/json', Authorization => 'Basic ' . encode_base64($username . ':' . +$password) }; my $client = REST::Client->new(); $client->setHost($jiraUrl); my $description = qq( Application : $app Node : $node OBJECT: $object SEVERITY : $severity GROUP : $group Submitted by : $user Alert Received on : $received MSG ID : $id {code} $msg_text {code} ); # escaping stuff... $summary =~ s/([^\x20-\x7e])/sprintf '\u%04x', ord($1)/ge; $description =~ s/([^\x20-\x7e])/sprintf '\u%04x', ord($1)/ge; my $jsonparams = qq({ "fields": { "project": {"key": "MCTEST"},"summary": "$summary", "description": "$description", "issuetype": {"name": "Incident"},"customfield_1 +23060": [{"value": "Foo"}, {"value": "BAR"}, {"value": "CHOO"}]}}); $client->POST('/rest/api/2/issue/', $jsonparams, $headers); my $res_data = from_json($client->responseContent()); my $issuekey = ''; $issuekey = $res_data->{'key'}; if ($issuekey =~ /TEST/) { print qq(Ticket http://example.jira.com/browse/$issuekey created.); } else { print " There was a problem. Here are some details: \n"; print Dumper $client->responseCode(); print Dumper $client->responseContent(); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to insert a string unknown contents into json consistently?
by Anonymous Monk on Apr 22, 2015 at 03:18 UTC | |
by Anonymous Monk on Apr 22, 2015 at 04:00 UTC |