Curl thowing error while trying to query JIRA with strings in field filter - perl

I need some help with my perl script.
I am new to perl and using curl and despite a lot of searching/ trial and error I am still stumped.
I am trying to use CURL to retrieve a set of jira issues based on the provided JQL.
Unfortunately I am having issues with spaces when I search fields like summary:
my $jql = JiraUrl.'search?jql=summary~"Some%20Silly%20Issue"';
When I curl this...
my $jsonResponse = `curl -D- -u $user:$password -X GET -H "Content-Type: application/json" $jql`;
I get an error ..
{"errorMessages":["Error in the JQL Query: Expecting either \'OR\' or \'AND\' but got \'Silly\'. (line 1, character 77)"],"errors":{}}';
Basicly when I escape the " " with %20 it ignores the "'s
Any help for how to get around this?

It looks like it's a shell escaping issue. The quotes are being interpreted by the shell. Try this:
my $jsonResponse = `curl -D- -u $user:$password -X GET -H "Content-Type: application/json" '$jql'`;

Related

Calling REST API with special characters in data parameters from Perl is failing

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

MarkLogic ingest JSON from external API

I am using Marklogic 9 and try to ingest data from external source into MarkLogic. I made an REST API on port 8031. When I try to execute the following curl command:
curl --anyauth --user admin:admin -i -X POST -d https://services7.arcgis.com/21GdwfcLrnTpiju8/arcgis/rest/services/Geluidsbelasting/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json
-H "Content-type: application/json" -H "Accept: application/json" \
'http://localhost:8031
After executing this statement I receive the error:
Curl: URL is not specified
Can you please help me out!
Many thanks
Erik
Your -d parameter has special characters that are not escaped. Try putting quotes around your -d url. It will prevent your command from getting truncated and misinterpreted at & signs..
HTH!

Curl with restful API

My pc is windows 10. I want to use curl command to get the data from elasticsearch, but I have some problems.
this is my curl command (a small test):
curl "http://localhost:9200/_search" -d '{"query": {"match_all": {}}}'
and the cmd show the error:
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}curl: (3) [globbing] unmatched brace in column 1
curl: (3) [globbing] empty string within braces in column 2
could anyone help me?
thank you in advance.
It seems like you need to provide the proper Content-Type header as well:
curl -H "Content-Type: application/json" -d '{"query": {"match_all": {}}}' "http://localhost:9200/_search"

Creating JIRA issue using curl from command line

I've been through documentation here according to which I'm creating a issue for JIRA . I know I'm making some terribly small mistake .
I'm trying to create a new JIRA request from command line (later I'll integrate in my java code)
From my Mac terminal I'm trying to run :
curl -D- -u username:password -X POST --data {"fields":{"project":{"key": “PROJECTKEY"},"summary": "REST ye merry gentlemen.","description": "Creating of an issue using project keys and issue type names using the REST API","issuetype": {"name": "Bug"}}} -H "Content-Type: application/json" https://mycompanyname.atlassian.net/rest/api/2/issue/
I believe this has something to do with the "data".
Thanks in advance. The example has been taken from the documentation link itself.
OUTPUT : I'm getting nothing in my terminal, no error, no expected output .
PROJECTKEY is taken from the KEY column from the All Project list in my DASHBOARD.
Two things are off:
you need to put the data that you want to post in quotes
the first double quote surrounding PROJECT_KEY is a unicode character instead of a regular double quote, so change “PROJECTKEY" to "PROJECTKEY"
This should work:
curl -D- -u username:password -X POST --data '{"fields":{"project":{"key": "PROJECTKEY"},"summary": "REST ye merry gentlemen.","description": "Creating of an issue using project keys and issue type names using the REST API","issuetype": {"name": "Bug"}}}' -H "Content-Type: application/json" https://mycompanyname.atlassian.net/rest/api/2/issue/

Unable to use CURL within GROOVY script for a REST PUT call

I am trying to do a simple PUT request using CURL. Simple it is on a terminal but not able to get it working within my Groovy script.
Here is a snippet of it :-
class Test {
//Throws 415 Cannot Consume Content Type
void testPUT () {
println "curl -i -X PUT -H \"Content-Type: application/json\" -d '{\"Key1\":1, \"Key2\":\"Value2\"}' http://<hostname>/foo/".execute().text
}
// Works Perfectly Fine
void testGET () {
println "curl -i -X GET -H \"Content-Type: application/json\" http://<hostname>/foo".execute().text
}
}
I also tried to enclose the command using triple quotes like:-
"""curl -i -X PUT -H "Content-Type:application/json" -d '{"Key1":1,"Key2":"Value2"}' http://<hostname>/foo""".execute().text
All my attempts just gives 415 Content Type Cannot be Consumed
When I simply use the curl command on a terminal window, both PUT and GET methods work fine.
Am I missing something? Any help would be appreciated!
Thank You!
Try using the list variation of the string and see if that works:
println ["curl", "-i", "-X PUT", "-H 'Content-Type:application/json'", "-d '{\"Key1\":1, \"Key2\":\"Value2\"}'", "http://<hostname>/foo/"].execute().text
I was having a similar problem and this was the only way I could find to solve it. Groovy will split the string into arguments at each space and I suspect this was tripping up Curl and the -H argument pair. By putting the string into the list variant, it keeps each item together as an argument.
Building on Bruce's answer, you will also need to tokenize "-X PUT". Tested out on groovy 2.3.6. ["curl", "-H", "Content-Type: application/json", "-H", "Accept: application/json", "-X", "PUT", "-d", data, uri].execute()
This works in my terminal
groovy -e "println 'curl -i -H \'Content-Type:application/json\' -XPUT -d \'{\"test\":4}\' http://google.fr/'.execute().text"
If it does not work for you, then this is likely not a groovy problem.
Thanks xynthy for the list variation hint, I was still seeing the dreaded
"Content type 'application/x-www-form-urlencoded' not supported"
with your example, but breaking up the -H and the content type strings worked.
This is confirmed working in groovy 1.8:
["curl", "-H", "Content-Type: application/json", "-H", "Accept: application/json", "-X PUT", "-d", data, uri].execute().text
First I installed the groovy post build plugin
https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin
Then I have included groovy post build plugin in my post build configuration of my jenkins job
and used the command
"curl --request POST http://172.16.100.101:1337/jenkins/build".execute().text
Here my endpoint is http:172.16.100.101:1337/jenkins/build