Objective-C
Objective-C
Lightspeed - Create a Product
See more Lightspeed Examples
Create a new product based on the given parameters.Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <CkoHttpResponse.h>
#import <CkoStringBuilder.h>
#import <NSString.h>
BOOL success = NO;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoHttp *http = [[CkoHttp alloc] init];
// Implements the following CURL command:
// curl -u API_KEY:API_SECRET \
// -H "Content-Type: application/json" \
// -X POST \
// -d '{
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }' \
// "https://api.webshopapp.com/en/products.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.
// {
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"product.visibility" value: @"visible"];
[json UpdateString: @"product.data01" value: @""];
[json UpdateString: @"product.data02" value: @""];
[json UpdateString: @"product.data03" value: @""];
[json UpdateString: @"product.title" value: @"Lookin Sharp T-Shirt"];
[json UpdateString: @"product.fulltitle" value: @"Lookin Sharp T-Shirt"];
[json UpdateString: @"product.description" value: @"Description of the Lookin Sharp T-Shirt"];
[json UpdateString: @"product.content" value: @"Long Description of the Lookin Sharp T-Shirt"];
[json UpdateInt: @"product.deliverydate" value: [NSNumber numberWithInt: 6488]];
[json UpdateInt: @"product.supplier" value: [NSNumber numberWithInt: 78794]];
[json UpdateInt: @"product.brand" value: [NSNumber numberWithInt: 1171202]];
[http SetRequestHeader: @"Content-Type" value: @"application/json"];
// Use the correct cluster for your shop. Here are the choices:
// eu1 https://api.webshopapp.com/en/
// us1 https://api.shoplightspeed.com/en/
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpJson: @"POST" url: @"https://api.webshopapp.com/en/products.json" json: json contentType: @"application/json" response: resp];
if (success == NO) {
NSLog(@"%@",http.LastErrorText);
return;
}
CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
[resp GetBodySb: sbResponseBody];
CkoJsonObject *jResp = [[CkoJsonObject alloc] init];
[jResp LoadSb: sbResponseBody];
jResp.EmitCompact = NO;
NSLog(@"%@",@"Response Body:");
NSLog(@"%@",[jResp Emit]);
int respStatusCode = [resp.StatusCode intValue];
NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
if (respStatusCode >= 400) {
NSLog(@"%@",@"Response Header:");
NSLog(@"%@",resp.Header);
NSLog(@"%@",@"Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "product": {
// "id": 20974460,
// "createdAt": "2019-05-28T20:14:26+00:00",
// "updatedAt": "2019-05-28T20:14:26+00:00",
// "isVisible": true,
// "visibility": "visible",
// "hasMatrix": false,
// "data01": "",
// "data02": "",
// "data03": "",
// "url": "lookin-sharp-t-shirt",
// "title": "Lookin' Sharp T-Shirt",
// "fulltitle": "Lookin' Sharp T-Shirt",
// "description": "Description of the Lookin' Sharp T-Shirt",
// "content": "<p>Long Description of the Lookin' Sharp T-Shirt</p>",
// "set": false,
// "brand": {
// "resource": {
// "id": 1171202,
// "url": "brands/1171202",
// "link": "https://api.shoplightspeed.com/us/brands/1171202.json"
// }
// },
// "categories": {
// "resource": {
// "id": false,
// "url": "categories/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/categories/products.json?product=20974460"
// }
// },
// "deliverydate": {
// "resource": {
// "id": 6488,
// "url": "deliverydates/6488",
// "link": "https://api.shoplightspeed.com/us/deliverydates/6488.json"
// }
// },
// "image": false,
// "images": false,
// "relations": {
// "resource": {
// "id": false,
// "url": "products/20974460/relations",
// "link": "https://api.shoplightspeed.com/us/products/20974460/relations.json"
// }
// },
// "metafields": {
// "resource": {
// "id": false,
// "url": "products/20974460/metafields",
// "link": "https://api.shoplightspeed.com/us/products/20974460/metafields.json"
// }
// },
// "reviews": {
// "resource": {
// "id": false,
// "url": "reviews?product=20974460",
// "link": "https://api.shoplightspeed.com/us/reviews.json?product=20974460"
// }
// },
// "type": false,
// "attributes": {
// "resource": {
// "id": false,
// "url": "products/20974460/attributes",
// "link": "https://api.shoplightspeed.com/us/products/20974460/attributes.json"
// }
// },
// "supplier": {
// "resource": {
// "id": 78794,
// "url": "suppliers/78794",
// "link": "https://api.shoplightspeed.com/us/suppliers/78794.json"
// }
// },
// "tags": {
// "resource": {
// "id": false,
// "url": "tags/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/tags/products.json?product=20974460"
// }
// },
// "variants": {
// "resource": {
// "id": false,
// "url": "variants?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants.json?product=20974460"
// }
// },
// "movements": {
// "resource": {
// "id": false,
// "url": "variants/movements?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants/movements.json?product=20974460"
// }
// }
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
int productId = [[jResp IntOf: @"product.id"] intValue];
NSString *productCreatedAt = [jResp StringOf: @"product.createdAt"];
NSString *productUpdatedAt = [jResp StringOf: @"product.updatedAt"];
BOOL productIsVisible = [jResp BoolOf: @"product.isVisible"];
NSString *productVisibility = [jResp StringOf: @"product.visibility"];
BOOL productHasMatrix = [jResp BoolOf: @"product.hasMatrix"];
NSString *productData01 = [jResp StringOf: @"product.data01"];
NSString *productData02 = [jResp StringOf: @"product.data02"];
NSString *productData03 = [jResp StringOf: @"product.data03"];
NSString *productUrl = [jResp StringOf: @"product.url"];
NSString *productTitle = [jResp StringOf: @"product.title"];
NSString *productFulltitle = [jResp StringOf: @"product.fulltitle"];
NSString *productDescription = [jResp StringOf: @"product.description"];
NSString *productContent = [jResp StringOf: @"product.content"];
BOOL productSet = [jResp BoolOf: @"product.set"];
int productBrandResourceId = [[jResp IntOf: @"product.brand.resource.id"] intValue];
NSString *productBrandResourceUrl = [jResp StringOf: @"product.brand.resource.url"];
NSString *productBrandResourceLink = [jResp StringOf: @"product.brand.resource.link"];
BOOL productCategoriesResourceId = [jResp BoolOf: @"product.categories.resource.id"];
NSString *productCategoriesResourceUrl = [jResp StringOf: @"product.categories.resource.url"];
NSString *productCategoriesResourceLink = [jResp StringOf: @"product.categories.resource.link"];
int productDeliverydateResourceId = [[jResp IntOf: @"product.deliverydate.resource.id"] intValue];
NSString *productDeliverydateResourceUrl = [jResp StringOf: @"product.deliverydate.resource.url"];
NSString *productDeliverydateResourceLink = [jResp StringOf: @"product.deliverydate.resource.link"];
BOOL productImage = [jResp BoolOf: @"product.image"];
BOOL productImages = [jResp BoolOf: @"product.images"];
BOOL productRelationsResourceId = [jResp BoolOf: @"product.relations.resource.id"];
NSString *productRelationsResourceUrl = [jResp StringOf: @"product.relations.resource.url"];
NSString *productRelationsResourceLink = [jResp StringOf: @"product.relations.resource.link"];
BOOL productMetafieldsResourceId = [jResp BoolOf: @"product.metafields.resource.id"];
NSString *productMetafieldsResourceUrl = [jResp StringOf: @"product.metafields.resource.url"];
NSString *productMetafieldsResourceLink = [jResp StringOf: @"product.metafields.resource.link"];
BOOL productReviewsResourceId = [jResp BoolOf: @"product.reviews.resource.id"];
NSString *productReviewsResourceUrl = [jResp StringOf: @"product.reviews.resource.url"];
NSString *productReviewsResourceLink = [jResp StringOf: @"product.reviews.resource.link"];
BOOL productType = [jResp BoolOf: @"product.type"];
BOOL productAttributesResourceId = [jResp BoolOf: @"product.attributes.resource.id"];
NSString *productAttributesResourceUrl = [jResp StringOf: @"product.attributes.resource.url"];
NSString *productAttributesResourceLink = [jResp StringOf: @"product.attributes.resource.link"];
int productSupplierResourceId = [[jResp IntOf: @"product.supplier.resource.id"] intValue];
NSString *productSupplierResourceUrl = [jResp StringOf: @"product.supplier.resource.url"];
NSString *productSupplierResourceLink = [jResp StringOf: @"product.supplier.resource.link"];
BOOL productTagsResourceId = [jResp BoolOf: @"product.tags.resource.id"];
NSString *productTagsResourceUrl = [jResp StringOf: @"product.tags.resource.url"];
NSString *productTagsResourceLink = [jResp StringOf: @"product.tags.resource.link"];
BOOL productVariantsResourceId = [jResp BoolOf: @"product.variants.resource.id"];
NSString *productVariantsResourceUrl = [jResp StringOf: @"product.variants.resource.url"];
NSString *productVariantsResourceLink = [jResp StringOf: @"product.variants.resource.link"];
BOOL productMovementsResourceId = [jResp BoolOf: @"product.movements.resource.id"];
NSString *productMovementsResourceUrl = [jResp StringOf: @"product.movements.resource.url"];
NSString *productMovementsResourceLink = [jResp StringOf: @"product.movements.resource.link"];