Skip to content

Effortless HTTP Requests in Xojo: Using Curl in Xojo with URLConnection

You’re building a fantastic Xojo application that needs to connect to the internet, grab data from websites, or send information to web services. This is where HTTP requests come in. They’re the language your application uses to talk to the web.

If you’re familiar with the command-line tool curl, you may wonder how to use curl in Xojo effectively. Curl is a powerful tool for making HTTP requests, but it’s not always the most convenient for working within a graphical application like Xojo.

That’s where Xojo’s URLConnection class shines! It provides a simple and elegant way to make HTTP requests directly from your Xojo code, making it easy to use curl in Xojo.

In this post, you will learn how to adapt common curl commands to Xojo’s URLConnection. Let’s get started.


Basic GET Request Using Curl in Xojo

Meaning: Fetch the content of the specified URL using a GET request.

Curl:

curl https://example.com

Xojo:

Var socket As New URLConnection
Var response As String = socket.SendSync("GET", "https://example.com", 30)

GET Request with Query Parameters

Meaning: Fetch the content of the specified URL with query parameters.

Curl:

curl "https://example.com?param1=value1&param2=value2"

Xojo:

Var socket As New URLConnection
Var response As String = socket.SendSync("GET", "https://example.com?param1=value1&param2=value2", 30)

Or:

Var socket As New URLConnection

// Define the query parameters using Pair
Var queryParams() As Pair
queryParams.Add("param1" : "value1")
queryParams.Add("param2" : "value2")

// Construct the URL with query parameters
Var baseURL As String = "https://example.com"
Var queryString As String = "?"

For Each param As Pair In queryParams
  queryString = queryString + param.Left + "=" + param.Right + "&"
Next

// Remove the trailing '&' character
queryString = queryString.Left(queryString.Length - 1)

// Combine base URL and query string
Var fullURL As String = baseURL + queryString

Var response As String = socket.SendSync("GET", fullURL, 30)

Adding Headers

Meaning: Send a GET request with a custom header.

Curl:

curl -H "Content-Type: application/json" https://example.com

Xojo:

Var socket As New URLConnection
socket.RequestHeader("Content-Type") = "application/json"
Var response As String = socket.SendSync("GET", "https://example.com", 30)

Sending JSON Data with POST

Meaning: Send a POST request with JSON data to the specified URL.

Curl:

curl -L 'http://example.com' ^
-H 'Content-Type: application/json' ^
-d '{"key1":"value1","key2":"value2"}'

Xojo:

Var socket As New URLConnection
Var jsonData As String = "{""key1"":""value1"",""key2"":""value2""}"
socket.SetRequestContent(jsonData, "application/json")
Var response As String = socket.SendSync("POST", "https://example.com", 30)

Or:

Var socket As New URLConnection
// Create a new JSONItem
Var json As New JSONItem

// Add key-value pairs to the JSONItem
json.Value("key1") = "value1"
json.Value("key2") = "value2"

socket.SetRequestContent(json.ToString, "application/json")
Var response As String = socket.SendSync("POST", "https://example.com", 30)

Basic Authentication

Meaning: Send a GET request with basic authentication.

Curl:

curl -u username:password http://example.com

Xojo:

Var socket As New URLConnection
socket.RequestHeader("Authorization") = "Basic " + EncodeBase64("username:password")
Var response As String = socket.SendSync("GET", "https://example.com", 30)

Download a File

Meaning: Download a file from the specified URL and save it with the given filename.

Curl:

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

Xojo:

Var socket As New URLConnection
Var outputFile As FolderItem = SpecialFolder.Desktop.Child("file.zip")
socket.SendSync("GET", "https://example.com/file.zip", outputFile, 30)

Custom Request Methods

Meaning: Send a DELETE request to the specified URL (you can replace DELETE with any other method required, PUT, PATCH…)

Curl:

curl -X DELETE https://example.com/resource

Xojo:

Var socket As New URLConnection
Var response As String = socket.SendSync("DELETE", "https://example.com/resource", 30)

As you’ve seen, using curl in Xojo with the URLConnection class is a breeze.

Whether you’re fetching data, sending/receiving JSON payloads, or downloading files, the examples provided in this tutorial offer a solid foundation for integrating web services into your Xojo applications. As you continue to experiment with URLConnection, you’ll discover even more ways to enhance your projects and improve your workflow.

If you found this tutorial helpful, don’t forget to share it with your fellow developers and colleagues.

Gabriel is a digital marketing enthusiast who loves coding with Xojo to create cool software tools for any platform. He is always eager to learn and share new ideas!