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

httpRequest method in Pipeline causes weird behavior

    Details

    • Similar Issues:

      Description

      We're running into an issue with httpRequest in a Groovy script used on a mutlibranch pipeline job.

      I have code that looks something like this:

      /**** Jenkinsfile ****/
      // ...
      notify = load 'notify.groovy'
      // ...
      if (checkResults(...) > 0) {
          println 'Tests failed, stopping build'
          return 1
      }
      
      int checkResults(...) {
          // ...
          notify.buildFailed(...)
      
          return failedTestCount
      }
      
      /**** notify.groovy ****/
      
      void buildFailed(...) {
        // ...
        sendMessage(...)
      }
      
      void sendMessage(...) {
         // ...
        def response = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON',
          httpMode: 'POST', requestBody: body, consoleLogResponseBody: true,
          url: 'https://example.net/hooks/4ezwZwLYbmYwJmD6MXN6yH3YNdd/C6m5uKJstArcKQyxfBYb5nMW7PkPSriACsXg9w',
          validResponseContent: '"success":true'
      
        println "Sent a notification, got a $response.status response"
      }
      

      Expected Results

      When this code runs, I expect for the httpRequest to return the response, the code to get printed out from my println, then checkResults to return the number of failed tests it detected (code not shown). However, that's not what happens.

      Actual Results

      Instead, sendMessage immediately return without executing my println, and most oddly of all, checkResults ends up returning the HTTP Response from httpRequest! How is this even possible?

      The symptom, other than my println doing nothing, is that the > 0 comparison in the Jenkinsfile doesn't work and this exception is thrown:

      groovy.lang.GroovyRuntimeException: Cannot compare jenkins.plugins.http_request.ResponseContentSupplier with value 'Status: 200, Response: {"success":true}' and java.lang.Integer with value '0'
      

      It's possible that I'm doing something wrong here, but this seems very much like a bug. It's hard for me to imagine what I could do wrong that would cause the stack to get blown apart in such a way.

        Attachments

          Activity

          Hide
          janario Janario Oliveira added a comment - - edited

          I've tried it and It is working ok

           

          def body = 'test'
          def response = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON',
            httpMode: 'POST', 
            requestBody: body, consoleLogResponseBody: true,
            url: 'http://requestb.in/1ecf72r1',
            validResponseContent: 'ok'
          println "Sent a notification, got a $response response"
          

          This will log:

           

          Running: Perform an HTTP Request and return a response object
          HttpMode: POST
          URL: http://requestb.in/1ecf72r1
          Content-type: application/json
          Accept: application/json
          Sending request to url: http://requestb.in/1ecf72r1
          Response Code: HTTP/1.1 200 OK
          Response: 
          ok
          Success code from [100‥399]
          Running: Print Message
          Sent a notification, got a Status: 200, Response: ok response
          Running: End of Workflow
          Finished: SUCCESS

           

          Note that I changed validResponseContent: '"success":true' to validResponseContent: 'ok' otherwise I receive an error

           

          ERROR: Fail: Response with length 2 doesn't contain '"success":true'

           

          Tyrel Haveman

          Can you confirm if the response content has '"success":true'?

          Can you attach the full job log?

           

          Show
          janario Janario Oliveira added a comment - - edited I've tried it and It is working ok   def body = 'test' def response = httpRequest acceptType: 'APPLICATION_JSON' , contentType: 'APPLICATION_JSON' ,   httpMode: 'POST' ,   requestBody: body, consoleLogResponseBody: true ,   url: 'http: //requestb.in/1ecf72r1' ,   validResponseContent: 'ok' println "Sent a notification, got a $response response" This will log:   Running: Perform an HTTP Request and return a response object HttpMode: POST URL: http: //requestb.in/1ecf72r1 Content-type: application/json Accept: application/json Sending request to url: http: //requestb.in/1ecf72r1 Response Code: HTTP/1.1 200 OK Response: ok Success code from [100‥399] Running: Print Message Sent a notification, got a Status: 200, Response: ok response Running: End of Workflow Finished: SUCCESS   Note that I changed  validResponseContent: '"success":true' to validResponseContent: 'ok' otherwise I receive an error   ERROR: Fail: Response with length 2 doesn 't contain ' "success" : true '   Tyrel Haveman Can you confirm if the response content has '"success":true' ? Can you attach the full job log?  
          Hide
          tyrelh Tyrel Haveman added a comment -

          It has been over four months since I reported this issue. I no longer have the job log available. We moved on by making the HTTP request using HttpsUrlConnection directly instead of this plugin and that has been working fine for the past four months.

          I just reverted back to using httpRequest and it now appears to be working fine. So wherever the bug is must has been fixed sometime in the last four months.

          Show
          tyrelh Tyrel Haveman added a comment - It has been over four months since I reported this issue. I no longer have the job log available. We moved on by making the HTTP request using HttpsUrlConnection directly instead of this plugin and that has been working fine for the past four months. I just reverted back to using httpRequest and it now appears to be working fine. So wherever the bug is must has been fixed sometime in the last four months.
          Hide
          janario Janario Oliveira added a comment -

          Thanks, 

          Please, let me know, if it happens again.

          Show
          janario Janario Oliveira added a comment - Thanks,  Please, let me know, if it happens again.

            People

            • Assignee:
              janario Janario Oliveira
              Reporter:
              tyrelh Tyrel Haveman
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: