Java
Chilkat2 Java
Akeneo: Create New Attribute
See more HTTP Misc Examples
Demonstrates how to create a new attribute.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
// Use your previously obtained access token.
// See Get Akeneo Access Token
http.setAuthToken("access_token");
// Build the following JSON to be sent in the request body:
// Use this online tool to generate the code from sample JSON:
// Generate Code to Create JSON
// {
// "code": "release_date",
// "type": "pim_catalog_date",
// "group": "marketing",
// "unique": false,
// "useable_as_grid_filter": true,
// "allowed_extensions": [],
// "metric_family": null,
// "default_metric_unit": null,
// "reference_data_name": null,
// "available_locales": [],
// "max_characters": null,
// "validation_rule": null,
// "validation_regexp": null,
// "wysiwyg_enabled": null,
// "number_min": null,
// "number_max": null,
// "decimals_allowed": null,
// "negative_allowed": null,
// "date_min": "2017-06-28T08:00:00",
// "date_max": "2017-08-08T22:00:00",
// "max_file_size": null,
// "minimum_input_length": null,
// "sort_order": 1,
// "localizable": false,
// "scopable": false,
// "labels": {
// "en_US": "Sale date",
// "fr_FR": "Date des soldes"
// }
// }
JsonObject json = new JsonObject();
json.updateString("code", "release_date");
json.updateString("type", "pim_catalog_date");
json.updateString("group", "marketing");
json.updateBool("unique", false);
json.updateBool("useable_as_grid_filter", true);
json.updateNewArray("allowed_extensions");
json.updateNull("metric_family");
json.updateNull("default_metric_unit");
json.updateNull("reference_data_name");
json.updateNewArray("available_locales");
json.updateNull("max_characters");
json.updateNull("validation_rule");
json.updateNull("validation_regexp");
json.updateNull("wysiwyg_enabled");
json.updateNull("number_min");
json.updateNull("number_max");
json.updateNull("decimals_allowed");
json.updateNull("negative_allowed");
json.updateString("date_min", "2017-06-28T08:00:00");
json.updateString("date_max", "2017-08-08T22:00:00");
json.updateNull("max_file_size");
json.updateNull("minimum_input_length");
json.updateNumber("sort_order", "1");
json.updateBool("localizable", false);
json.updateBool("scopable", false);
json.updateString("labels.en_US", "Sale date");
json.updateString("labels.fr_FR", "Date des soldes");
json.setEmitCompact(false);
// Show the JSON to be sent..
System.out.println(json.emit());
String url = "http://pim.my-akeneo-site.com/api/rest/v1/attributes";
HttpResponse resp = new HttpResponse();
try {
http.httpJson("POST", url, json, "application/json", resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Response Status Code: " + resp.getStatusCode());
System.out.println("Response Body: ");
System.out.println(resp.getBodyStr());
}
}