Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-18407

Starting a job with params using REST Api doesn't return 201 http status code as stated in the documentation

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Critical Critical
    • core
    • Linux

      This bug is related to the new enhancement that was added in v1.519 for better tracking a queued job.
      When starting the job with parameters I expect to get 201 status code, but instead I'm getting 302 code which redirects me to the "Location" header url.
      See attached screenshot from the request and the response (taken by Fiddler)

          [JENKINS-18407] Starting a job with params using REST Api doesn't return 201 http status code as stated in the documentation

          Amos Sonnenwirth created issue -

          joe miller added a comment -

          I'm seeing strange behavior here as well. When starting a job with params via the UI, sometimes the result is a '201 CREATED' (and a blank/white page in the browser), and sometimes a 302 redirect back to the job page as in previous versions.

          joe miller added a comment - I'm seeing strange behavior here as well. When starting a job with params via the UI, sometimes the result is a '201 CREATED' (and a blank/white page in the browser), and sometimes a 302 redirect back to the job page as in previous versions.

          To explain what's going on, 201 gets returned when the item gets submitted in the queue, and 302 if the item was already in the queue.

          I realize that in both cases it probably makes sense to return 201, even though in the latter case, technically we haven't created any resources. Most often, the caller doesn't really care if the submission resulted in a new item in the queue or if it was already in the queue.

          Kohsuke Kawaguchi added a comment - To explain what's going on, 201 gets returned when the item gets submitted in the queue, and 302 if the item was already in the queue. I realize that in both cases it probably makes sense to return 201, even though in the latter case, technically we haven't created any resources. Most often, the caller doesn't really care if the submission resulted in a new item in the queue or if it was already in the queue.
          Kohsuke Kawaguchi made changes -
          Assignee New: Kohsuke Kawaguchi [ kohsuke ]

          Code changed in jenkins
          User: Kohsuke Kawaguchi
          Path:
          core/src/main/java/hudson/matrix/MatrixConfiguration.java
          core/src/main/java/hudson/matrix/listeners/MatrixBuildListener.java
          core/src/main/java/hudson/model/AbstractProject.java
          core/src/main/java/hudson/model/Queue.java
          core/src/main/java/hudson/model/queue/ScheduleResult.java
          test/src/test/groovy/hudson/model/AbstractProjectTest.groovy
          http://jenkins-ci.org/commit/jenkins/f719bd06a46b0c8f8249f5d4848db61947b920be
          Log:
          [FIXED JENKINS-18407]

          Added Queue.schedule2 to allow the caller to retrieve the existing item in the queue. AbstractProject.doBuild() changed the behavior a bit to reply 201 if the item was already found in the queue (instead of a new one created.)

          Compare: https://github.com/jenkinsci/jenkins/compare/bdfc3f715630...f719bd06a46b

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Kohsuke Kawaguchi Path: core/src/main/java/hudson/matrix/MatrixConfiguration.java core/src/main/java/hudson/matrix/listeners/MatrixBuildListener.java core/src/main/java/hudson/model/AbstractProject.java core/src/main/java/hudson/model/Queue.java core/src/main/java/hudson/model/queue/ScheduleResult.java test/src/test/groovy/hudson/model/AbstractProjectTest.groovy http://jenkins-ci.org/commit/jenkins/f719bd06a46b0c8f8249f5d4848db61947b920be Log: [FIXED JENKINS-18407] Added Queue.schedule2 to allow the caller to retrieve the existing item in the queue. AbstractProject.doBuild() changed the behavior a bit to reply 201 if the item was already found in the queue (instead of a new one created.) Compare: https://github.com/jenkinsci/jenkins/compare/bdfc3f715630...f719bd06a46b
          SCM/JIRA link daemon made changes -
          Resolution New: Fixed [ 1 ]
          Status Original: Open [ 1 ] New: Resolved [ 5 ]

          dogfood added a comment -

          Integrated in jenkins_main_trunk #2661

          Result = UNSTABLE

          dogfood added a comment - Integrated in jenkins_main_trunk #2661 Result = UNSTABLE

          I used the latest build (not official one) in order to test the fix, and this time I'm getting HTTP CODE 200

          I created a simple job with one string parameter and used the following code:

          import java.io.IOException;
          import java.util.ArrayList;
          import java.util.HashMap;
          import java.util.List;
          import java.util.Map;
          import java.util.Map.Entry;

          import org.codehaus.jackson.JsonFactory;
          import org.codehaus.jackson.JsonNode;
          import org.codehaus.jackson.JsonProcessingException;
          import org.codehaus.jackson.map.ObjectMapper;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.http.HttpEntity;
          import org.springframework.http.HttpHeaders;
          import org.springframework.http.HttpMethod;
          import org.springframework.http.HttpStatus;
          import org.springframework.http.MediaType;
          import org.springframework.http.ResponseEntity;
          import org.springframework.stereotype.Component;
          import org.springframework.util.LinkedMultiValueMap;
          import org.springframework.util.MultiValueMap;
          import org.springframework.web.client.RestTemplate;

          .
          .
          .
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

          MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
          form.add("COUNT", "5");

          HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(form,headers);

          ResponseEntity<String> responce = restTemplate.exchange("http://192.168.1.126:8080/jenkins/job/test1/buildWithParameters", HttpMethod.POST, request, String.class);
          .
          .
          .

          The returned HTTP code is 200 instead of 201 (saw it on Fiddler)

          Additionally, I'm getting a json result which contains the "queueItem" section, together with other non-relevant info, I can use this "queueItem" info for tracking the job, BUT when starting few instances of the same job in parallel (each instance with different parameters), in each HTTP response I'm getting the same "queueItem" as the first one I got, although, when checking the queue the other new instance were created with different item id in the queue

          If this too complicated to understand, lets try to set a webex session or something similar (my mail: amos_son@malam.com)

          Amos Sonnenwirth added a comment - I used the latest build (not official one) in order to test the fix, and this time I'm getting HTTP CODE 200 I created a simple job with one string parameter and used the following code: import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; . . . HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); form.add("COUNT", "5"); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(form,headers); ResponseEntity<String> responce = restTemplate.exchange("http://192.168.1.126:8080/jenkins/job/test1/buildWithParameters", HttpMethod.POST, request, String.class); . . . The returned HTTP code is 200 instead of 201 (saw it on Fiddler) Additionally, I'm getting a json result which contains the "queueItem" section, together with other non-relevant info, I can use this "queueItem" info for tracking the job, BUT when starting few instances of the same job in parallel (each instance with different parameters), in each HTTP response I'm getting the same "queueItem" as the first one I got, although, when checking the queue the other new instance were created with different item id in the queue If this too complicated to understand, lets try to set a webex session or something similar (my mail: amos_son@malam.com)
          Amos Sonnenwirth made changes -
          Resolution Original: Fixed [ 1 ]
          Status Original: Resolved [ 5 ] New: Reopened [ 4 ]

          I confirm that using the REST API, I get a 200 instead of a 201 when performing a build. The URL I call is :
          https://<hidden>/jenkins/job/<JOB>/buildWithParameters?module=amoreTST

          Barthélémy von Haller added a comment - I confirm that using the REST API, I get a 200 instead of a 201 when performing a build. The URL I call is : https://<hidden>/jenkins/job/<JOB>/buildWithParameters?module=amoreTST

            kohsuke Kohsuke Kawaguchi
            amoss166 Amos Sonnenwirth
            Votes:
            6 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: