Sample code for 30+ languages & platforms
Swift

Explaining the Rest ClearResponseBodyStream Method

See more Azure Cloud Storage Examples

The ClearResponseBodyStream method would be needed if your applicaiton called SetResponseBodyStream to send the response to a stream for one request, but then wants to subsequently do something else. The application must call ClearResponseBodyStream to no longer send responses to the stream.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let rest = CkoRest()!

    // Connect to the web server
    var bTls: Bool = true
    var port: Int = 443
    var bAutoReconnect: Bool = true
    success = rest.connect(hostname: "www.chilkatsoft.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    // Setup a file stream for the download
    let fileStream = CkoStream()!
    fileStream.sinkFile = "qa_output/starfish.jpg"

    // Indicate that the call to FullRequestNoBody should send the response body
    // to fileStream if the response status code is 200.
    // If a non-success response status code is received, then nothing
    // is streamed to the output file and the error response is returned by FullRequestNoBody.
    var expectedStatus: Int = 200
    rest.setResponseBodyStream(expectedStatus: expectedStatus, autoSetStreamCharset: true, responseStream: fileStream)

    var responseStr: String? = rest.fullRequestNoBody(httpVerb: "GET", uriPath: "/images/starfish.jpg")
    if rest.lastMethodSuccess == false {
        // Examine the request/response to see what happened.
        print("response status code = \(rest.responseStatusCode.intValue)")
        print("response status text = \(rest.responseStatusText!)")
        print("response header: \(rest.responseHeader!)")
        print("response body (if any): \(responseStr!)")
        print("---")
        print("LastRequestStartLine: \(rest.lastRequestStartLine!)")
        print("LastRequestHeader: \(rest.lastRequestHeader!)")
        return
    }

    print("downloaded starfish.jpg")

    // Clear the response body stream previously set in the call to SetResponseBodyStream.
    rest.clearResponseBodyStream()

    // Download something else, but this time send the response body to bd.
    let bd = CkoBinData()!
    success = rest.fullRequestNoBodyBd(httpVerb: "GET", uriPath: "/images/penguins.jpg", binData: bd)
    if success == false {
        // Examine the request/response to see what happened.
        print("response status code = \(rest.responseStatusCode.intValue)")
        print("response status text = \(rest.responseStatusText!)")
        print("response header: \(rest.responseHeader!)")
        print("response body (if any): \(bd.getString(charset: "utf-8")!)")
        print("---")
        print("LastRequestStartLine: \(rest.lastRequestStartLine!)")
        print("LastRequestHeader: \(rest.lastRequestHeader!)")
        return
    }

    // Save the contents of bd to a file.
    success = bd.writeFile(path: "qa_output/penguins.jpg")

    print("Success: \(success)")

}