How to Check Multiple Text Inputs for SQL Injection Attacks in Swift

Cloudmersive
2 min readMay 3, 2021

With online commerce at an all-time high, a cyber threat that targets data fields for contact, checkout, or other forms is bound to be running wild. The SQL injection attack is such a threat, and has been reported as impacting or at least attempting to impact a large majority of businesses. Protecting your organization from these threats is rising in importance, and in this brief tutorial we will provide an API that can be used to detect SQL injections from multiple text inputs in batch.

All you’ll need to run the API in Swift is your batch request and your API key to input into the following code:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = "{\n \"RequestItems\": [\n {\n \"InputText\": \"<string>\"\n },\n {\n \"InputText\": \"<string>\"\n }\n ],\n \"DetectionLevel\": \"<string>\"\n}"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/validate/text-input/check/sql-injection/batch")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()

As a reference, here is an example of a batch request input:

{
"RequestItems": [
{
"InputText": "string"
}
],
"DetectionLevel": "string"
}

And the process is complete after that — quick, painless, and secure.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.