Setting custom properties on SonarQube projects - api

I'm following the documentation on the Sonarqube web api for getting/setting properties. I'd like to set a property on project pi_core with the property name "appName" and the value "UCFE". Ultimately I want to fully automate this via PowerShell but for now I'm just trying to validate the concepts using curl just like the docs. My command is:
curl -u myID:myPassword -X POST http://myServer.ad1.prod:9000/api/properties?id=appName&value=UCFE&resource=pi_core
I've verified that my ID and password work by executing other generic web api calls that require admin authorization. When I try to run the above I get:
{"err_code":200,"err_msg":"property created"}'value' is not recognized as an internal or external command, operable program or batch file.
'resource' is not recognized as an internal or external command, operable program or batch file.
Any ideas why this command, which seems to me to be identical to the documentation except for the values, gets the above error?

To start with, close the URI in quotes;
curl -u myID:myPassword -X POST 'http://myServer.ad1.prod:9000/api/properties?id=appName&value=UCFE&resource=pi_core'
The & for a start will send 3 commands into the background if you don't enclose it and run that from a shell
API Documentation is so often incomplete, has limited testing, is thrown together, or even unintentionally gets caught in the escaping process when displaying on blogs.
When using curl on the command line, remember that the shell interprets the command first.
Your initial command: curl -u myID:myPassword -X POST http://myServer.ad1.prod:9000/api/properties?id=appName&value=UCFE&resource=pi_core, looks like this to the shell:
arg1: curl
arg2: -u
arg3: myID:myPassword
arg4: -X
arg5: POST
arg6: http://myServer.ad1.prod:9000/api/properties?id=appName
arg7: value=UCFE
arg8: resource=pi_core
Therefore, arg7, and arg8 above, were sent to cURL as arguments. cURL wouldn't have a clue what "value=UCFE" means, hence the error:
'value' is not recognized as an internal or external command
When you put quotes around a block of text that you want sent as one argument (say, the URL to cURL), it looks like this:
Command: curl -u myID:myPassword -X POST 'http://myServer.ad1.prod:9000/api/properties?id=appName&value=UCFE&resource=pi_core'
Interpreted:
Arg0: curl
Arg1: -u
Arg2: myID:myPassword
Arg3: -X
Arg4: POST
Arg5: http://myServer.ad1.prod:9000/api/properties?id=appName&value=UCFE&resource=pi_core
Sidenote - for the sake of completion;
It often doesn't matter whether the quotes are single or double, but they are different.
Case for single quotes: Some APIs, have $ signs in them. For example MYOB has many parameters like the following; In this case, if there are many of them, it may be easier to use single quotes: '?$filter=DateOccurred%20ge%20datetime%272016-07-25%2'. In this case, if there are many of them, it may be easier to use single quotes.
Case for double quotes: Lots of variables: "?id=$id&refresh=true&paramlist=$params&authredirect=$authlevel"

Related

Is possible to execute curl in Karate tests?

I need to run some test that use a NTLM proxy.
Due to Karate doesn´t support NTLM proxy, I think that if karate can "execute" a curl command like below, I will get kate working with NTLM:
curl -X GET 'https://someaddress.com/cats?Status=completed' -u siteuser:sitepasswd --proxy-ntlm --proxy-user ckuser:ckpasswd --proxy internal-ntlm-proxy:8080 -s
Anyone knows if I can call a curl command in Karate? (instead of the internal http request that Karate use when call Given... Path...)
Thanks
Yes, Karate has very good CLI support, if curl is present on your OS, it can be done. See this answer for details, available in 0.9.6 https://stackoverflow.com/a/62911366/143475
In your case, try first with karate.exec()
* def result = karate.exec("curl -X GET 'https://someaddress.com/cats?Status=completed' -u siteuser:sitepasswd --proxy-ntlm --proxy-user ckuser:ckpasswd --proxy internal-ntlm-proxy:8080 -s")
And result will contain the console text. Note that there are regex helpers to make scraping values out easier, for e.g.:
* def token = karate.extract(result, 'some(pattern).+', 1)

How to delete a Jelastic environment through the API?

I tried to delete one of my Jelastic environments by means of the following API call:
curl -k \
-H "${CONTENT_TYPE}" \
-A "${USER_AGENT}" \
-X POST \
-fsS ${HOSTER_URL}/1.0/environment/control/rest/deleteenv -d "password=${password}&session=${session}&envName=${envName}
where I am sure that the session and envName are correct, as I have other commands running perfectly well with them. In particular, I get the session in the following way:
getSession() {
local login=$1
local password=$2
local hosterUrl=$3
echo "Signing in..." >&2
local cmd=$(curl -k -H "${CONTENT_TYPE}" -A "${USER_AGENT}" -X POST \
-fsS "$hosterUrl/1.0/users/authentication/rest/signin" -d "login=$login&password=$password");
exitOnFail $cmd
echo "Signed in" >&2
echo $(jq '.session' <<< $cmd | sed 's/\"//g')
}
In the call to deleteenv, I provide the very same password as that of my Jelastic provider account. Indeed, when I want to delete an environment through Jelastic's dashboard, this is the password I need to provide to make the deletion happen. However, I get the following error:
{"result":801,"source":"hx-core","error":"invalid password"}
Because the password field is documented as optional in Jelastic's API documentation, I tried not to set the password. This yields the following error:
{"result":3,"source":"JEL","error":"invalid parameter [password] for method [DeleteEnv] in service [ControlService]"}
I tried to use other secrets as that password, like the APPID, without any success.
Does anyone have a clue what password I need to put there?
We used the same oneliner based on API you used, your script to get a session and the same Jelastic version but were not able to reproduce this issue. Environment were successfully deleted.
Does anyone have a clue what password I need to put there?
This is the same password as you used to get session or to enter Dashboard. Make sure there are no additional symbols in your password variable.
Because the password field is documented as optional
Indeed, it's optional if use token instead of session. In case of session, password is an obligatory parameter.

Read API data using curl with GET method

I am trying to read open source data available on "https://swapi.co/" using curl.
there is no authentication needed and only the GET Method could be used to query the schema and data according to their documentation.
I am having an error on the following command:
curl -x GET "http://swapi.co/api/"
I am getting the following error message
C:\Users*****>curl -x GET "http://swapi.co/api/"
curl: (5) Could not resolve proxy: GET.
Any idea?
kindest regards
-x (lower case x) is for setting a proxy, and you told it the proxy is named GET. (Which obviously was wrong since it can't resolve such a host name.)
Remove the "-x GET" and it'll work.

Gitlab API: How to generate the private token

This is what I tried:
curl http://git.ep.petrobras.com.br/api/v3/session --data-urlencode 'login=myUser&password=myPass'
Answer:
{"message":"401 Unauthorized"}
The problem is the data-urlencode CURL option. Since it's an HTTP POST you don't need to URL encode the data, and is actually encoding the & into & and causing your issue. Instead use the --data option.
curl http://git.ep.petrobras.com.br/api/v3/session --data 'login=myUser&password=myPass'
Also, be careful sending credentials over plain HTTP. It could be easily sniffed.
This is how:
$ curl http://git.ep.petrobras.com.br/api/v3/session/ --data-urlencode 'login=myUser' --data-urlencode 'password=myPass'
The solution pointed out by Steven doesn't work if your username or password contains characters that have to be urleencoded. The name=content format will urlencode the content part (the name part has to be urlencoded but login and password are fine).
To actually retrieve the private_token you can pipe the output of curl into jq like this:
$ curl [as above] | jq --raw-output .private_token
x_the_private_token_value_x
This way you can easily use it in a shell script.
Also, as Steven pointed out already, please use https instead so that your password is not transmitted in clear text across the wire.
Note: this workflow no longer works as of GitLab 8.6.0 as the default password has been removed.
Changelog: https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CHANGELOG#L205
I only just noticed this and raised the issue. Leaving this note here to hopefully save someone else some time. Hopefully, this is a decision that will be reviewed and reverted.
Discussion/issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/1980

HTTP PUT deleting triples in sesame

I'm trying to use http PUT via cURL to update my triple store in openrdf-sesame, but I've hit a problem that I can't find a solution for.
When using POST, the triple data uploads perfectly.
But then using PUT, instead of adding the data provided, it deletes all the data from my repository. I've ran cURL in verbose mode, and it's giving back the expected HTTP status code.
I've added
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
to Apache Tomcat's web.xml,
Ive also tried the guide here: HTTP PUT Guide, but that hasn't helped either.
EDIT
Here are the cURL commands from my batch file:
call "%curl%" -# -X POST %endpoint%/statements -H "Content-Type:application/x-trig;charset=UTF-8" -d #%%X
call "%curl%" -# -X PUT %endpoint%/statements -H "Content-Type:application/x-trig;charset=UTF-8" -d #%%X
Which is essentially:
curl.exe -# -X POST http://myendpoint/statements -H "Content-Type:application/x-trig;charset=UTF-8" -d #MyTrigFile.trig
curl.exe -# -X PUT http://myendpoint/statements -H "Content-Type:application/x-trig;charset=UTF-8" -d #MyTrigFile.trig
For the moment, I've disabled authentication, but otherwise I'd also include a -u user:password argument.
I have just ran some tests using your exact way of invoking curl, and I can't reproduce the problem (using Sesame version 2.6.8): it works as expected here.
The only possible cause I can think of is that your TriG file has a syntax error which Sesame fails to report back.
Another possible problem is your expectations: the command as you execute it will completely clear the entire repository and replace the data with the contents of your TriG file. Were you perhaps expecting something else?
To further debug this, can you have a look in the Sesame server logs and tell us if it shows any warnings or errors? Server logs are viewable by going to http://<server:port>/openrdf-sesame/ in the browser and clicking 'System' in the menu on the left.