Skip to main content
TopMiniSite

Back to all posts

How to Use Curl Command In Groovy Script?

Published on
3 min read

Table of Contents

Show more
How to Use Curl Command In Groovy Script? image

To use the curl command in a Groovy script, you can leverage the ProcessBuilder class to execute the curl command. This involves creating a new ProcessBuilder object with the command and its arguments, starting the process, and reading the output if needed. For example, you can execute a simple GET request using curl like this:

def url = "https://api.example.com/data" def processBuilder = new ProcessBuilder("curl", url) Process process = processBuilder.start() InputStream inputStream = process.getInputStream() BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)) String line while ((line = reader.readLine()) != null) { println line } Remember to handle any errors that may occur during the execution of the curl command in your script.

How to download a file using cURL?

To download a file using cURL, you can use the following command:

curl -o filename.extension https://example.com/file.extension

In this command:

  • -o filename.extension specifies the output filename and extension of the downloaded file.
  • https://example.com/file.extension is the URL of the file you want to download.

You can replace filename.extension and https://example.com/file.extension with your desired filename and URL. After running this command, the file will be downloaded to the current directory.

How to use cURL to send a PUT request?

To send a PUT request using cURL, you can use the following command:

curl -X PUT -H "Content-Type: application/json" -d '{"key": "value"}' http://example.com/api/resource

This command includes the following options:

  • -X PUT: Specifies that a PUT request should be sent.
  • -H "Content-Type: application/json": Sets the Content-Type header to application/json.
  • -d '{"key": "value"}': Specifies the data to be included in the request body in JSON format.
  • http://example.com/api/resource: Specifies the URL of the resource to which the PUT request should be sent.

Replace the URL and JSON data with your specific values when using this command.

How to use cURL in a Groovy script?

You can use cURL in a Groovy script by executing cURL commands using the ProcessBuilder class. Here's an example of how you can do this:

def url = "https://api.example.com" def command = ["curl", url]

// Create a new ProcessBuilder def processBuilder = new ProcessBuilder(command)

// Start the process def process = processBuilder.start()

// Get the output of the command def inputStream = process.getInputStream() def reader = new BufferedReader(new InputStreamReader(inputStream))

// Read the output line by line String line while ((line = reader.readLine()) != null) { println line }

// Close the reader reader.close()

In this example, we first define the URL that we want to make a cURL request to. We then create a ProcessBuilder object with the cURL command and URL as arguments. We then start the process and get the output of the command using an InputStream and BufferedReader. Finally, we read the output line by line and print it to the console.

You can also pass additional options to the cURL command by adding them to the command list before creating the ProcessBuilder object.