Sample code for 30+ languages & platforms
Swift

HTTP GET with Non-USASCII Query Params

See more HTTP Examples

This example illustrates how query parameters in a URL are typically encoded and transmitted.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    // First, let's load the string "Grünhöfer GmbH" from a file.  (This is a fictitious company name.)
    // The file uses the utf-8 charset encoding.
    let sbCompanyName = CkoStringBuilder()!
    success = sbCompanyName.loadFile(path: "qa_data/txt/companyName.txt", charset: "utf-8")
    // Assuming success for this example...

    // We'll send an HTTP GET request to https://chilkatsoft.com/example?company_name={company name}

    // When sending an HTTP GET request with query parameters that contain accented characters 
    // (e.g., umlauts: ä, ö, ü), they must be percent-encoded (URL encoded) to ensure proper transmission and 
    // interpretation by the server.  This is based on their UTF-8 byte values.
    let sbUrl = CkoStringBuilder()!
    sbUrl.append(value: "https://chilkatsoft.com/example?company_name=")
    sbUrl.append(value: sbCompanyName.getEncoded(encoding: "url", charset: "utf-8"))

    let http = CkoHttp()!

    // Send the following HTTP GET request:

    // GET /example?company_name=Gr%C3%BCnh%C3%B6fer%20GmbH HTTP/1.1
    // Host: chilkatsoft.com
    // Accept: */*
    // Accept-Encoding: gzip

    let sbResponse = CkoStringBuilder()!
    success = http.quickGetSb(url: sbUrl.getAsString(), sbContent: sbResponse)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("response status code: \(http.lastStatus.intValue)")
    print("\(sbResponse.getAsString()!)")

}