Search code examples
curlsonarqubesonarqube-web

Setting custom properties on SonarQube projects


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?


Solution

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

    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:


    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"