Sample code for 30+ languages & platforms
Swift

Shopware 6 - Create Category

See more Shopware 6 Examples

Create a new category.

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()!

    // Sends the following POST

    // POST /api/v3/category
    // {
    //     "name": "Test123"
    // }

    // Create a category named "Test123"
    let json = CkoJsonObject()!
    json.updateString(jsonPath: "name", value: "Test123")

    // Load the access token previously obtained in Shopware 6 OAuth2 Client Credentials
    let jsonToken = CkoJsonObject()!
    jsonToken.loadFile(path: "qa_data/tokens/shopware6.json")

    // This causes the "Authorization: Bearer <access_token>" header to be added.
    http.authToken = jsonToken.string(of: "access_token")

    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: "https://my-shopware-6-shop.de/api/v3/category", 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

    // A successful response is a 204 response status code with an empty response body.
    print("Response Body:")
    print("\(jResp.emit()!)")

    // If we get a 401 response, it may be that our access token expired and we need to fetch a new one.
    var respStatusCode: Int = resp.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp.header!)")
        print("Failed.")
        return
    }


}