Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No response if "_streaming" is false #829

Open
HassanTaleb90 opened this issue May 5, 2024 · 0 comments
Open

No response if "_streaming" is false #829

HassanTaleb90 opened this issue May 5, 2024 · 0 comments

Comments

@HassanTaleb90
Copy link

If "_streaming" is set to false, there will be no response. However, if it is set to true, everything works as expected:

Response: {"kind":"version","text":"Swift version 5.9.2 (swift-5.9.2-RELEASE)\nTarget: x86_64-unknown-linux-gnu\n"}
{"kind":"stdout","text":"Hello, Swift!\n"}

Code:

func sendPostRequest(urlString: String, headers: [String: String], jsonObject: [String: Any], completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
    // Convert the JSON object to Data
    guard let jsonData = try? JSONSerialization.data(withJSONObject: jsonObject) else {
        print("Error converting JSON object to data")
        return
    }

    // Create the URL
    guard let url = URL(string: urlString) else {
        print("Invalid URL")
        return
    }

    // Create the request
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.allHTTPHeaderFields = headers
    request.httpBody = jsonData

    // Create the URLSession and task
    let session = URLSession.shared
    let task = session.dataTask(with: request, completionHandler: completion)

    // Start the task
    task.resume()
}

// Example usage
let url = "https://swiftfiddle-runner-functions-592.blackwater-cac8eec1.westus2.azurecontainerapps.io/runner/5.9.2/run"

let headers = [
    "Content-Type": "application/json; charset=UTF-8"
]
let script = "print(\"Hello, Swift!\")"
let jsonObject: [String : Any] = [
    "code": script,
    "toolchain_version": "5.9.2",
    "_color": false,
    "_streaming": false
]

sendPostRequest(urlString: url, headers: headers, jsonObject: jsonObject) { (data, response, error) in
    if let error = error {
        print("Error: \(error)")
    } else if let data = data {
        if let resultString = String(data: data, encoding: .utf8) {
            print("Response: \(resultString)")
        } else {
            print("Unable to convert data to string")
        }
    } else {
        print("No data received")
    }
}

Note: I observed that the URL request for all versions follows a similar pattern, such as https://swiftfiddle-runner-functions-592.blackwater-cac8eec1.westus2.azurecontainerapps.io/runner/5.9.2/run, except for version 5.10, which utilizes a different format: https://swiftfiddle-runner.onrender.com/runner/5.10.0/run. Is there a method to utilize a uniform URL structure, similar to the previous one, such as https://swiftfiddle.com/runner/5.9.2/run?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant