Sample code for 30+ languages & platforms
Swift

POST JSON Without Waiting for the Response

See more REST Examples

This example demonstrates sending a POST w/ content-type "application/json" where the body of the POST contains a JSON document. The POST is sent, but we don't wait for the response.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let rest = CkoRest()!

    // Connect to the server using TLS
    var bAutoReconnect: Bool = false
    success = rest.connect(hostname: "example.com", port: 443, tls: true, autoReconnect: bAutoReconnect)
    if success == false {
        print("\(rest.lastErrorText!)")
        return
    }

    // Create some sample JSON to be sent in the body of the request.
    let sbJson = CkoStringBuilder()!
    sbJson.append(value: "{\"create\": [{\"name\": \"Woo Single #1\",\"type\": \"simple\",\"regular_price\": \"21.99\"}]}")

    // Add the Content-Type HTTP request header.
    rest.addHeader(name: "Content-Type", value: "application/json")

    // Just send the request.  Don't read the response.
    // The request is sent to https://example.com/something?arg1=xyz&arg2=abc
    var somePath: String? = "/something?arg1=xyz&arg2=abc"
    success = rest.sendReqSb(httpVerb: "POST", uriPath: somePath, bodySb: sbJson)
    if success == false {
        print("\(rest.lastErrorText!)")
        return
    }

    // OK, the request was sent.
    // Close the connection.
    var maxWaitMs: Int = 50
    rest.disconnect(maxWaitMs: maxWaitMs)

    print("JSON POST Sent.")

}