Hi, I’m trying to trigger a build on our server but I keep getting an internal error when I’m trying to pass parameters in. FYI, I’m using URLRequest in Swift. I read somewhere that the parameters need to be sent as queries so the URL goes as follows: “http:SERVER_URL:8080/PATH/BUILD?PARAM_KEY=PARAM_VALUE&…” I put the URL together with percent encoding. I put the Authorization token in the header; I’m unsure if there’s other specific headers that are required.
The server responds with 500 and a bunch of HTML code in the body.
If I put the parameters in the body, it’s successful but the job doesn’t actually use them.
Here’s a quick guide on how to trigger a build on your server using URLRequest in Swift, and how to pass parameters as query strings. Make sure your URL is well formatted and your headers are set up correctly. Let’s dive into the details:
Create the URL with query parameters: Start by ensuring the URL is correctly percent-encoded.
Set up the URLRequest: Add all the necessary headers, like the Authorization token.
Send the request: Use URLSession to fire off the request and handle whatever response comes back.
Example in Swift:
import Foundation
// Define the server URL and parameters
let serverURL = "http://SERVER_URL:8080/PATH/BUILD"
let parameters = [
"PARAM_KEY1": "PARAM_VALUE1",
"PARAM_KEY2": "PARAM_VALUE2"
]
// Create the URL with query parameters
var urlComponents = URLComponents(string: serverURL)!
urlComponents.queryItems = parameters.map { URLQueryItem(name: $0.key, value: $0.value) }
guard let url = urlComponents.url else {
fatalError("Invalid URL")
}
// Create the URLRequest
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Bearer YOUR_AUTH_TOKEN", forHTTPHeaderField: "Authorization")
// Create the URLSession
let session = URLSession.shared
// Send the request
let task = session.dataTask(with: request) { data, response, error in
if let error = error {
print("Error: \(error)")
return
}
guard let httpResponse = response as? HTTPURLResponse else {
print("Invalid response")
return
}
if httpResponse.statusCode == 200 {
print("Build triggered successfully")
} else {
print("Failed to trigger build. Status code: \(httpResponse.statusCode)")
if let data = data, let responseBody = String(data: data, encoding: .utf8) {
print("Response body: \(responseBody)")
}
}
}
task.resume()
What we did here:
URL with Query Parameters: We used URLComponents to include our parameters in the URL.
URLRequest Setup: Configured the request to use POST method and included an Authorization header.
Sending the Request: Finally, sent the request using URLSession and processed the results.
Just replace YOUR_AUTH_TOKEN with your actual token and tweak the serverURL and parameters to fit your needs. This should definitely help you trigger the build with your desired parameters.
Thanks but the following keeps failing: /job/Parameterized%20Demo%20Job%201/buildWithParameters?MY_NAME_IS=Darth%20Vador&U_KEELED_MY=Death%20Star&IS_COMEDY=true HTTP/1.1
For anyone coming on this later, this was resolved by the suggestion. I failed to realize that the parameters have hard-coded options and only passing in those options will have success.