Automating HTTP Requests In Jenkins: Sending And Handling Responses

In the world of continuous integration and continuous delivery (CI/CD), Jenkins has been widely adopted as a popular automation server. It provides numerous plugins and features that enable developers to automate various tasks in the software development lifecycle. One of the key capabilit

In the world of continuous integration and continuous delivery (CI/CD), Jenkins has been widely adopted as a popular automation server. It provides numerous plugins and features that enable developers to automate various tasks in the software development lifecycle. One of the key capabilities of Jenkins is its ability to make HTTP requests to external services. In this article, we will explore how to use Jenkins to send HTTP requests and handle the responses.

Understanding Jenkins HTTP Request Plugin
Jenkins provides a plugin called “HTTP Request” that allows you to send HTTP requests to external services. This plugin can be easily installed and configured in your Jenkins environment. The plugin supports different types of HTTP requests such as GET, POST, PUT, DELETE, etc. Additionally, it also supports advanced features like providing request headers, query parameters, request body, handling response codes, and saving response data.

Installing the HTTP Request Plugin
To install the HTTP Request plugin in Jenkins, follow these steps:

1. Navigate to the Jenkins dashboard and click on “Manage Jenkins” on the left-hand side menu.

2. Select “Manage Plugins” from the dropdown menu.

3. In the “Available” tab, search for “HTTP Request” using the search bar.

4. Check the checkbox next to the “HTTP Request” plugin.

5. Click on the “Install” without restart button to install the plugin.

Once the plugin is installed, you can now start using it in your Jenkins jobs.

Sending HTTP Request from a Jenkins Job
To send an HTTP request from a Jenkins job, you need to configure the job with the necessary information. Let’s take a look at the steps to configure an HTTP request in a Jenkins job:

1. Create a new or navigate to an existing Jenkins job.

2. Click on “Configure” to edit the job settings.

3. Scroll down to the “Build” section and click on “Add build step” button.

4. Select “Send an HTTP request to a URL” from the dropdown menu.

5. Enter the URL of the target service in the “URL” field.

6. Select the HTTP method (GET, POST, etc.) from the “HTTP Mode” dropdown.

7. Optionally, provide request headers and query parameters if required.

8. If you need to send a request body, select “Use request body” and enter the body content.

9. Specify the response code to check for success or failure using the “Valid Response Codes” field.

10. Optionally, you can save the response data to a file by providing the “Output File Path”.

That’s it! You have now configured Jenkins to send an HTTP request to the target service. When the job is executed, Jenkins will make the HTTP request and handle the response based on the configured settings.

Handling HTTP Response
Once Jenkins makes an HTTP request, it receives a response from the target service. Based on the response status code and content, you can perform different actions. The “HTTP Request” plugin provides various options to handle the response. Let’s explore a few of them:

1. Mark the build as failed: You can configure Jenkins logs to mark the build as failed if the response status code does not match the expected success code. For example, you can specify a list of response codes (e.g., 200, 201) that indicate a successful request. If the actual response code does not match any of these codes, the build will be marked as failed.
2. Extract information from the response: Sometimes, you may need to extract specific information from the response and use it in subsequent steps of the
. The plugin allows you to capture and save response data to environment variables or files. You can then use these variables/files in other build steps.
3. Assertions and verifications: If you want to verify certain values or conditions in the response, you can use assertions. The plugin provides options to assert response status code, response body content, response headers, etc. If the assertion fails, the build will be marked as failed.
Example Usage
To showcase the usage of the “HTTP Request” plugin, let’s consider an example where we want to trigger a deployment pipeline in a remote system via an HTTP request. Here’s how you can configure the Jenkins job:

1. Create a new Jenkins job or navigate to an existing job.

2. In the job configuration, add a new build step of type “Send an HTTP request to a URL”.

3. Enter the URL of the deployment pipeline endpoint in the “URL” field.

4. Select the HTTP method as POST.

5. Specify the body content in the “Use request body” field. This can be a JSON/XML payload with the required deployment details.

6. Set the “Valid Response Codes” field to 200, which indicates a successful deployment request.

7. Optionally, if you want to save the response data to a file, provide the “Output File Path”.

When this job is executed, Jenkins will send an HTTP POST request to the deployment pipeline endpoint with the specified body content. It will then check the response code and mark the build as failed if it doesn’t match 200. You can also configure the job to extract specific information from the response and use it in subsequent build steps.

Conclusion
In this article, we have explored how to use the “HTTP Request” plugin in Jenkins to send HTTP requests to external services. We have seen how to install and configure the plugin in your Jenkins environment and how to handle the response from the target service. With the ability to send HTTP requests, Jenkins becomes a powerful tool for automating various tasks in the CI/CD pipeline. Whether it’s triggering deployments, API integrations, or testing external services, the “HTTP Request” plugin in Jenkins makes it easy to communicate with external systems.


Cloudastra Technology

206 Blog posts

Comments