Sample code for 30+ languages & platforms
Swift

Read the REST Response Body into a Stream

See more REST Examples

Demonstrates Rest.ReadRespBodyStream, which reads the response body into a Stream. The optional flag sets the stream character set from the response Content-Type charset for textual responses.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. The staged request model separates sending from reading: a SendReq method transmits the request, ReadResponseHeader reads the status line and headers, and a ReadResp method reads the body. This gives fine control over large or streamed messages compared with the single-call full-request methods.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let rest = CkoRest()!
    var bTls: Bool = true
    var bAutoReconnect: Bool = true
    success = rest.connect(hostname: "example.com", port: 443, tls: bTls, autoReconnect: bAutoReconnect)
    if success == false {
        print("\(rest.lastErrorText!)")
        return
    }

    //  The file paths are relative to the application's current working directory.  Absolute paths
    //  may also be used.  Supply the paths appropriate to your own environment.

    success = rest.sendReqNoBody(httpVerb: "GET", uriPath: "/api/images/1")
    if success == false {
        print("\(rest.lastErrorText!)")
        return
    }

    var statusCode: Int = rest.readResponseHeader().intValue
    if statusCode < 0 {
        print("\(rest.lastErrorText!)")
        return
    }

    //  Read the response body into a Stream.  Here the stream's sink is a local file, which is
    //  appropriate for large or binary responses.
    let respStream = CkoStream()!
    respStream.sinkFile = "qa_output/image.png"

    //  If the 2nd argument is true and the response is textual, the stream's character set is set from
    //  the response Content-Type charset.  It has no effect for binary responses.
    var bAutoSetCharset: Bool = true
    success = rest.readRespBodyStream(stream: respStream, autoSetStreamCharset: bAutoSetCharset)
    if success == false {
        print("\(rest.lastErrorText!)")
        return
    }

    print("Response body written to the stream sink.")

}