Sample code for 30+ languages & platforms
Swift

HTTP POST x-www-form-urlencoded

Demonstrates how to send a simple x-www-form-urlencoded POST.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let http = CkoHttp()!

    var jsonStr: String? = "{ some json ... }"

    let req = CkoHttpRequest()!
    // This query parameter just happens to be named "json" and contains JSON text.
    req.addParam(name: "json", value: jsonStr)

    // We can optionally add more query parameters. 
    req.addParam(name: "abc", value: "123")
    req.addParam(name: "xml", value: "<abc>123</abc>")

    // Note: Just because we passed a query param named "json" or "xml" means nothing special.  It's still just
    // a name=value query parameter..

    req.httpVerb = "POST"
    req.contentType = "application/x-www-form-urlencoded"

    let resp = CkoHttpResponse()!
    success = http.httpReq(url: "http://example.com/xyz/connect/report", request: req, response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    if resp.statusCode.intValue != 200 {
        print("Hey, I didn't receive the expected response status code!")
        print("status code = \(resp.statusCode.intValue)")
    }

    // Could be error text if the status code wasn't what we expected, or could be the response
    // body you're seeking..
    var responseBody: String? = resp.bodyStr
    print("\(responseBody!)")

    let fac = CkoFileAccess()!
    var filepath: String? = "some file path"
    success = fac.writeEntireTextFile(path: filepath, fileData: responseBody, charset: "utf-8", includePreamble: false)
    if success != true {
        print("\(fac.lastErrorText!)")
    }


}