Generating token with API from jenkins with http requests plugins fails with code 400

Im trying to create admin user token. Retreiving list of tokens works, but creation gives me 400 code.

Sonarqube 9.6.0-community with from docker (container name sonar-app-nodb)
Jenkins 2.418 (docker)
HTTP Request Plugin 1.18 HTTP Request

pipeline:

        stage('tokens'){
            steps{
                script{
                     def response = httpRequest(
                        acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON',
                        url:"${apiurl}/user_tokens/search",
                        authentication: 'sonarcreds',
                        httpMode:'GET'
                        )
                    println("Status: "+response.status)
                    println("Content: "+response.content)
                }
            }
            
        }
        
        stage('create token'){
            steps{
                script{
                     def response = httpRequest(
                        acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON',
                        url:"${apiurl}/user_tokens/generate",
                        authentication: 'sonarcreds',
                        httpMode:'POST',
                        requestBody: '{"name":"jenkinstoken","login":"admin", "type": "GLOBAL_ANALYSIS_TOKEN"}'
                        )
                    println("Status: "+response.status)
                    println("Content: "+response.content)
                }
            }
            
        }

log

HttpMethod: GET
URL: http://sonar-app-nodb:9000/api/user_tokens/search
Content-Type: application/json
Accept: application/json
Using authentication: sonarcreds
Sending request to url: http://sonar-app-nodb:9000/api/user_tokens/search
Response Code: HTTP/1.1 200 
Success: Status code 200 is in the accepted range: 100:399
[Pipeline] echo
Status: 200
[Pipeline] echo
Content: {"login":"admin","userTokens":[{"name":"jakistoken","createdAt":"2023-09-05T11:49:43+0000","type":"GLOBAL_ANALYSIS_TOKEN"}]}
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (create token)
[Pipeline] script
[Pipeline] {
[Pipeline] httpRequest
HttpMethod: POST
URL: http://sonar-app-nodb:9000/api/user_tokens/generate
Content-Type: application/json
Accept: application/json
Using authentication: sonarcreds
Sending request to url: http://sonar-app-nodb:9000/api/user_tokens/generate
Response Code: HTTP/1.1 400 
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
hudson.AbortException: Fail: Status code 400 is not in the accepted range: 100:399 while calling http://sonar-app-nodb:9000/api/user_tokens/generate
	at jenkins.plugins.http_request.HttpRequestExecution.responseCodeIsValid(HttpRequestExecution.java:470)
	at jenkins.plugins.http_request.HttpRequestExecution.processResponse(HttpRequestExecution.java:480)
	at jenkins.plugins.http_request.HttpRequestExecution.authAndRequest(HttpRequestExecution.java:376)
	at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:284)
Also:   hudson.remoting.Channel$CallSiteStackTrace: Remote call to JNLP4-connect connection from jenkins-slave1.dominiknet/172.19.0.5:53878
		at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1787)
		at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:356)
		at hudson.remoting.Channel.call(Channel.java:1003)
		at jenkins.plugins.http_request.HttpRequestStep$Execution.run(HttpRequestStep.java:404)
		at jenkins.plugins.http_request.HttpRequestStep$Execution.run(HttpRequestStep.java:383)
		at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
		at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
		at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
		at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
		at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
		at java.base/java.lang.Thread.run(Thread.java:833)
Also:   org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: f380b125-7bd5-412b-a59a-f0a0842cfeca
Caused: java.lang.IllegalStateException
	at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:287)
	at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:80)
	at hudson.remoting.UserRequest.perform(UserRequest.java:211)
	at hudson.remoting.UserRequest.perform(UserRequest.java:54)
	at hudson.remoting.Request$2.run(Request.java:377)
	at hudson.remoting.InterceptingExecutorService.lambda$wrap$0(InterceptingExecutorService.java:78)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at hudson.remoting.Engine$1.lambda$newThread$0(Engine.java:125)
	at java.base/java.lang.Thread.run(Unknown Source)
Finished: FAILURE

Ok i just found the solution. Just changed payload type from json to form and it works now

Hi,

I’m glad you worked through your problem. You should be aware that your version is past EOL. You should upgrade to either the latest version or the current LTS at your earliest convenience. Your upgrade path is:

9.6 β†’ 9.9.1 β†’ 10.2 (last step optional)

You may find these resources helpful:

If you have questions about upgrading, feel free to open a new thread for that here.

 
Ann

Thanks. Unfortunately its not my decision. Im dependent on the version provided by our platform engineers in our automation platform. I already asked them for update and they will do it as soon as they have time to test it’s backwards compatibility with the rest of the projects.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.