Sample code for 30+ languages & platforms
Swift

Lightspeed - Upload an Image

See more Lightspeed Examples

Create (uploads) a new product image.

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 http = CkoHttp()!

    // Implements the following CURL command:

    // curl   -u API_KEY:API_SECRET \
    //     -H "Content-Type: application/json" \
    //     -X POST \
    //     -d '{
    //   "productImage": {
    //     "attachment": "base64-encoded-contents",
    //     "filename": "the-filename-with-extension-goes-here.jpg"
    //   }
    // }' \
    // "https://api.webshopapp.com/en/products/product_id/images.json"

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    http.login = "API_KEY"
    http.password = "API_SECRET"

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "productImage": {
    //     "attachment": "base64-encoded-contents",
    //     "filename": "the-filename-with-extension-goes-here.jpg"
    //   }
    // }

    let bdImage = CkoBinData()!
    success = bdImage.loadFile(path: "qa_data/jpg/starfish.jpg")
    if success == false {
        print("Failed to load image file.")
        return
    }

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "productImage.attachment", value: bdImage.getEncoded(encoding: "base64"))
    json.updateString(jsonPath: "productImage.filename", value: "starfish.jpg")

    http.setRequestHeader(name: "Content-Type", value: "application/json")

    // The product ID in the URL is "112265200".
    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: "https://api.webshopapp.com/en/products/112265200/images.json", json: json, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp.getBodySb(sb: sbResponseBody)
    let jResp = CkoJsonObject()!
    jResp.loadSb(sb: sbResponseBody)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    var respStatusCode: Int = resp.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp.header!)")
        print("Failed.")
        return
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "productImage": {
    //     "id": 13362569,
    //     "sortOrder": 3,
    //     "createdAt": "2019-06-06T14:52:07+00:00",
    //     "updatedAt": "2019-06-06T14:52:07+00:00",
    //     "extension": "png",
    //     "size": 8016,
    //     "title": "logo",
    //     "thumb": "https://cdn.shoplightspeed.com/shops/000001/files/14271562/50x50x2/logo.png",
    //     "src": "https://cdn.shoplightspeed.com/shops/000001/files/14271562/logo.png"
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var productImageId: Int = jResp.int(of: "productImage.id").intValue
    var productImageSortOrder: Int = jResp.int(of: "productImage.sortOrder").intValue
    var productImageCreatedAt: String? = jResp.string(of: "productImage.createdAt")
    var productImageUpdatedAt: String? = jResp.string(of: "productImage.updatedAt")
    var productImageExtension: String? = jResp.string(of: "productImage.extension")
    var productImageSize: Int = jResp.int(of: "productImage.size").intValue
    var productImageTitle: String? = jResp.string(of: "productImage.title")
    var productImageThumb: String? = jResp.string(of: "productImage.thumb")
    var productImageSrc: String? = jResp.string(of: "productImage.src")

}