It's possible to test web service in playground?

Hello Swift's developers:


I try to test my code that consume a Web Service in a playground. It's possible?


My code is as follows:


let endPoint = "http://host/service"


if let url = URL(string: endPoint) {

let task = URLSession.shared.dataTask(with: url) {data, response, error in

guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {

print(response)

return

}

if let mimeType = httpResponse.mimeType, mimeType == "text,html",

let data = data,

let cadena = String(data: data, encoding: .utf8) {

DispatchQueue.main.async {

}

}

}

task.resume()

}


I execute the code and playground doesn't show me error but the web service doesn't execute.


Is there an error in the code?


Best regards,

Victor.

Replies

You need to set

needsIndefiniteExecution
so that the playground keep running long enough for the network request to complete. For example, this code:
import Foundation
import PlaygroundSupport

let url = URL(string: "https://example.com")!
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
URLSession.shared.dataTask(with: request) { (data, response, error) in
    if let error = error as NSError? {
        print("task transport error \(error.domain) / \(error.code)")
        return
    }
    let response = response as! HTTPURLResponse
    let data = data!
    print("task finished with status \(response.statusCode), bytes \(data.count)")
}.resume()

PlaygroundPage.current.needsIndefiniteExecution = true

ends up printing:

task finished with status 200, bytes 1256

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you so much for your reply Eskimo.


I clone your code in my playground.


I test 3 different web service, 2 respond correctly but other generate a issue (EXC_BAD_INSTRUCTION). I don't understand what's going on with the last web service.


Playground showe me the issue in the line:

let url = URL(string: "https://example.com")!


What's the error? Does it take time for the URL instance to evaluate the service?


Best regards,

Victor.

The

EXC_BAD_INSTRUCTION
hardware exception typically means that you’ve hit a Swift runtime trap, for example, you’ve accessed an array out of bounds. In this case, the fact that it’s happening on the URL creation code strongly suggests that
URL(string:)
has returned
nil
, and thus the force unwrap (the
!
operator) is trapping.
URL(string:)
doesn’t hit the network; the only reason it can return
nil
is if the string you supplied is not a valid URL. For example:
print(URL(string: "https://example.com"))   // Optional(https://example.com)
print(URL(string: "https://e%ample.com"))   // nil

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you once again eskimo.


I understand what's the problem.


As I mentioned, I have 3 Web Services, 2 soap and the last only shoot a print, I do not expect data.


I noticed, when the playground executes the line:

let url = URL(string: "http://host/service")!

that goes to consume web service and gets the output.


When I test soap web services, I can see the output of URL and when test the web service that print, the result = nil.


How can I run the printing service?


Best regards,

Victor.

I noticed, when the playground executes the line:

let url = URL(string: "http://host/service")!

that goes to consume web service and gets the output.

No, that’s not right.

URL(string:)
does not do any networking. It just constructs a
URL
value based on the input string.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"