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

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

Related

Can't download using Nexus 3 REST API and CURL

I'm trying to download the latest snapshot dependency of a zip on Nexus 3 (version 3.22.1-02) from a command line using curl:
curl -u username:password -X GET "https://mynexusserver/service/rest/v1/search/assets/download?sort=version&repository=snapshotsrepo&maven.groupId=mygroup&maven.artefactId=myartefact&maven.extension=zip" -H "accept: application/json" -o myartefact.zip
This request is similar to this example: http://help.sonatype.com/repomanager3/rest-and-integration-api/search-api#SearchAPI-DownloadingtheLatestVersionofanAsset but no result is returned, myartefact.zip is empty.
However with the same URL, my artefact is downloaded from a web browser or with gradle.
With curl the following command line is working fine, returning the list of all snapshot versions of my artefact:
curl -u username:password -X GET "https://mynexusserver/service/rest/v1/search/assets?sort=version&repository=snapshotsrepo&maven.groupId=mygroup&maven.artefactId=myartefact&maven.extension=zip" -H "accept: application/json" -o myartefact.zip
Downloading the artefact directly is working fine as well with a command line like:
curl -u username:password "https://mynexusserver/repository/snapshotsrepo/mygroup/batchfactory/myversion-SNAPSHOT/myartefact-myversion-mytimestamp.zip" -H "accept: application/json" -o myartefact.zip
Verbose logs (-v option) show the artefact is found (I get HTTP/1.1 302 Found message) but nothing is downloaded.
Using wget doesn't work any better, I can't even query the list of snapshot version of the artefact.
Am I missing something?
Thanks #Zeitounator, after adding "-L" the command line works fine. Considering code 302 this feels obvious now...
Nexus documentation should also probably be updated to add the "-L" option.

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!

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/

POST request with Powershell 2.0 using cURL

Scenario
Among other things, Powershell 2.0 doesn't have the useful cmdlet Invoke-RestMethod.
I can't upgrade to version 3 and most examples I've found use version 3.
I have found this article, which seems, however, too complicated for my simple scenario.
I need to write a Powershell script that POSTs data in Json format, e.g.
{"Id":5,"Email":"test#com","DataFields":null,"Status":0}
What I've tried
I am able to GET data. This is one of the scripts I have tried.
curl -v --user username:password https://api.dotmailer.com/v2/account-info
But, when I try to POST, I can't figure out where to put the body of the message in the script. This is what I've got so far:
curl -v -X POST -H "Accept: application/json" -H "Content-Type: application/json" -u username:password -d '{"Id":5,"Email":"test#com","OptInType":0,"EmailType":0, "DataFields":null,"Status":0}' https://api.dotmailer.com/v2/contacts
which returns the following error:
{"message":"Could not parse the body of the request based on the content type \"application/json\" ERROR_BODY_DOES_NOT_MATCH_CONTENT_TYPE"}*
Question
Can anyone advise on how to POST Json data from Powershell using cURL?
Any pointers to why I get the error I mentioned in the Waht I've tried section would be much appreciated.
Thanks.
Note that the question is about the curl.exe external program, not about PowerShell's Invoke-WebRequest cmdlet (which, unfortunately, is aliased to curl in later PowerShell versions, preempting calls to the external program unless the .exe extension is explicitly specified (curl.exe ...).
Unfortunately and unexpectedly, you have to \-escape embedded " instances in a string you pass as an argument to an external program.
Therefore, even though:
'{"Id":5,"Email":"test#com","DataFields":null,"Status":0}'
should work, it doesn't, due to a long-standing bug; instead, you must use:
'{\"Id\":5,\"Email\":\"test#com\",\"DataFields\":null,\"Status\":0}'
See this answer for more information.
From curl's man page it appears you need to use -d switch:
curl -v --user username:password -H "Content-Type: application/json" -d '{"Id":5,"Email":"test#com","DataFields":null,"Status":0}' https://api.dotmailer.com/v2/contacts

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

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'`;