Creating Sonarqube Rules using Json format

Must-share information (formatted with Markdown):

  • SonarQube Version : 8.5.1
  • We would like to automate rules creation and mapping to quality profile via Json foramt.
  • SonarQube rules creation works with below parameter but not working Json format
    curl -su admin:admin \ -X POST \ -d 'template_key=javascript%3ACommentRegularExpression&custom_key=customRuleTest&name=customRuleTest&markdown_description=My%20custom%20rule&severity=MAJOR' \ 'http://localhost:9000/api/rules/create'

===

Below options are tried.

  1. curl -v -su admin:password' --header "Content-Type: application/json" -X POST -d @sonarqube.json 'https://sonarqube.com/api/rules/create'

Content of sonarqube.json:

{ "custom_key": "key1", "key": "ClangTidy:test", "repo": "ClangTidy", "name": "custom", "severity": "INFO", "templateKey": "ClangTidy:CustomRuleTemplate", "type": "CODE_SMELL" }

Tried by passing option in curl command as well but didn’t work. It is not able to read the values to pass while creation and says not able to find “custom_key”.

Please help us to fix the issue.

Thanks in Advance!!!

Hey there.

The SonarQube Web API is simply not built to accept JSON payloads – you must use form-data or x-www-form-urlencoded (passing -d or -F options to curl, or just appending the query parameters to the URL)