Rust
Rust
WordPress Media Upload
See more WordPress Examples
Demonstrates how to upload a media file to WordPress.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// This example will use WordPress username / Application password authentication using the "Applications Password" plugin.
// See https://wordpress.org/plugins/application-passwords/
// Use your WordPress login, such as "admin", not the application name.
http.set_login("wp_username");
// Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
http.set_password("app_password");
http.set_basic_auth(true);
let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
// Use the Content-Type that corresponds to the type of file you are uploading.
// WordPress supports uploading the following file types:
// Images
//
// .jpg (image/jpg)
// .jpeg (image/jpeg)
// .png (image/png)
// .gif (image/gif)
// .ico (image/x-icon)
//
// Documents
//
// .pdf (applicatin/pdf)
// .doc, .docx (application/msword)
// .ppt, .pptx, .pps, .ppsx (application/mspowerpoint)
// .odt
// .xls, .xlsx (application/msexcel)
// .psd (??? image/vnd.adobe.photoshop,application/x-photoshop,application/photoshop,application/psd,image/psd)
//
// Audio
//
// .mp3 (audio/mpeg)
// .m4a
// .ogg
// .wav (audio/wav)
//
// Video
//
// .mp4, .m4v (video/mp4)
// .mov (video/quicktime)
// .wmv (video/x-ms-wmv)
// .avi (video/avi)
// .mpg (video/mpeg)
// .ogv
// .3gp
// .3g2
req.set_content_type("image/jpg");
req.set_path("/wp-json/wp/v2/media");
if req.load_body_from_file("qa_data/jpg/starfish.jpg").is_err() {
println!("{}", req.last_error_text());
return;
}
req.add_header("Content-Disposition", "attachment; filename=starfish.jpg");
req.add_header("Expect", "100-continue");
let resp = chilkat::HttpResponse::new();
if http.http_s_req("www.yourserver.com", 443, true, &req, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("HTTP response status: {}", resp.status_code());
let json_response = chilkat::JsonObject::new();
let _ = json_response.load(&resp.body_str());
json_response.set_emit_compact(false);
println!("{}", json_response.emit().unwrap_or_default());
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "id": 1915,
// "date": "2021-01-19T18:25:01",
// "date_gmt": "2021-01-20T01:25:01",
// "guid": {
// "rendered": "http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg",
// "raw": "http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg"
// },
// "modified": "2021-01-19T18:25:01",
// "modified_gmt": "2021-01-20T01:25:01",
// "slug": "starfish",
// "status": "inherit",
// "type": "attachment",
// "link": "https:\/\/cknotes.com\/starfish\/",
// "title": {
// "raw": "starfish",
// "rendered": "starfish"
// },
// "author": 1,
// "comment_status": "closed",
// "ping_status": "closed",
// "template": "",
// "meta": [
// ],
// "permalink_template": "https:\/\/cknotes.com\/?attachment_id=1915",
// "generated_slug": "starfish",
// "description": {
// "raw": "",
// "rendered": "<p class=\"attachment\"><a href='https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg'><img width=\"120\" height=\"120\" src=\"https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg\" class=\"attachment-medium size-medium\" alt=\"\" loading=\"lazy\" srcset=\"http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg 120w, http:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish-100x100.jpg 100w\" sizes=\"(max-width: 120px) 100vw, 120px\" \/><\/a><\/p>\n"
// },
// "caption": {
// "raw": "",
// "rendered": ""
// },
// "alt_text": "",
// "media_type": "image",
// "mime_type": "image\/jpeg",
// "media_details": {
// "width": 120,
// "height": 120,
// "file": "2021\/01\/starfish.jpg",
// "sizes": {
// "woocommerce_gallery_thumbnail": {
// "file": "starfish-100x100.jpg",
// "width": 100,
// "height": 100,
// "mime_type": "image\/jpeg",
// "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish-100x100.jpg"
// },
// "shop_thumbnail": {
// "file": "starfish-100x100.jpg",
// "width": 100,
// "height": 100,
// "mime_type": "image\/jpeg",
// "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish-100x100.jpg"
// },
// "full": {
// "file": "starfish.jpg",
// "width": 120,
// "height": 120,
// "mime_type": "image\/jpeg",
// "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg"
// }
// },
// "image_meta": {
// "aperture": "0",
// "credit": "",
// "camera": "",
// "caption": "",
// "created_timestamp": "0",
// "copyright": "",
// "focal_length": "0",
// "iso": "0",
// "shutter_speed": "0",
// "title": "",
// "orientation": "0",
// "keywords": [
// ]
// }
// },
// "post": null,
// "source_url": "https:\/\/cknotes.com\/wp-content\/uploads\/2021\/01\/starfish.jpg",
// "missing_image_sizes": [
// ],
// "_links": {
// "self": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media\/1915"
// }
// ],
// "collection": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media"
// }
// ],
// "about": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/attachment"
// }
// ],
// "author": [
// {
// "embeddable": true,
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"
// }
// ],
// "replies": [
// {
// "embeddable": true,
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=1915"
// }
// ],
// "wp:action-unfiltered-html": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media\/1915"
// }
// ],
// "wp:action-assign-author": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media\/1915"
// }
// ],
// "curies": [
// {
// "name": "wp",
// "href": "https:\/\/api.w.org\/{rel}",
// "templated": true
// }
// ]
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
let date_gmt = chilkat::DtObj::new();
let id = json_response.int_of("id");
let date = json_response.string_of("date").unwrap_or_default();
let _ = json_response.dt_of("date_gmt", false, &date_gmt);
let guid_rendered = json_response.string_of("guid.rendered").unwrap_or_default();
let guid_raw = json_response.string_of("guid.raw").unwrap_or_default();
let modified = json_response.string_of("modified").unwrap_or_default();
let modified_gmt = json_response.string_of("modified_gmt").unwrap_or_default();
let slug = json_response.string_of("slug").unwrap_or_default();
let status = json_response.string_of("status").unwrap_or_default();
let v_type = json_response.string_of("type").unwrap_or_default();
let link = json_response.string_of("link").unwrap_or_default();
let title_raw = json_response.string_of("title.raw").unwrap_or_default();
let title_rendered = json_response.string_of("title.rendered").unwrap_or_default();
let author = json_response.int_of("author");
let comment_status = json_response.string_of("comment_status").unwrap_or_default();
let ping_status = json_response.string_of("ping_status").unwrap_or_default();
let template = json_response.string_of("template").unwrap_or_default();
let permalink_template = json_response.string_of("permalink_template").unwrap_or_default();
let generated_slug = json_response.string_of("generated_slug").unwrap_or_default();
let description_raw = json_response.string_of("description.raw").unwrap_or_default();
let description_rendered = json_response.string_of("description.rendered").unwrap_or_default();
let caption_raw = json_response.string_of("caption.raw").unwrap_or_default();
let caption_rendered = json_response.string_of("caption.rendered").unwrap_or_default();
let alt_text = json_response.string_of("alt_text").unwrap_or_default();
let media_type = json_response.string_of("media_type").unwrap_or_default();
let mime_type = json_response.string_of("mime_type").unwrap_or_default();
let media_details_width = json_response.int_of("media_details.width");
let media_details_height = json_response.int_of("media_details.height");
let media_details_file = json_response.string_of("media_details.file").unwrap_or_default();
let media_details_sizes_woocommerce_gallery_thumbnail_file = json_response.string_of("media_details.sizes.woocommerce_gallery_thumbnail.file").unwrap_or_default();
let media_details_sizes_woocommerce_gallery_thumbnail_width = json_response.int_of("media_details.sizes.woocommerce_gallery_thumbnail.width");
let media_details_sizes_woocommerce_gallery_thumbnail_height = json_response.int_of("media_details.sizes.woocommerce_gallery_thumbnail.height");
let media_details_sizes_woocommerce_gallery_thumbnail_mime_type = json_response.string_of("media_details.sizes.woocommerce_gallery_thumbnail.mime_type").unwrap_or_default();
let media_details_sizes_woocommerce_gallery_thumbnail_source_url = json_response.string_of("media_details.sizes.woocommerce_gallery_thumbnail.source_url").unwrap_or_default();
let media_details_sizes_shop_thumbnail_file = json_response.string_of("media_details.sizes.shop_thumbnail.file").unwrap_or_default();
let media_details_sizes_shop_thumbnail_width = json_response.int_of("media_details.sizes.shop_thumbnail.width");
let media_details_sizes_shop_thumbnail_height = json_response.int_of("media_details.sizes.shop_thumbnail.height");
let media_details_sizes_shop_thumbnail_mime_type = json_response.string_of("media_details.sizes.shop_thumbnail.mime_type").unwrap_or_default();
let media_details_sizes_shop_thumbnail_source_url = json_response.string_of("media_details.sizes.shop_thumbnail.source_url").unwrap_or_default();
let media_details_sizes_full_file = json_response.string_of("media_details.sizes.full.file").unwrap_or_default();
let media_details_sizes_full_width = json_response.int_of("media_details.sizes.full.width");
let media_details_sizes_full_height = json_response.int_of("media_details.sizes.full.height");
let media_details_sizes_full_mime_type = json_response.string_of("media_details.sizes.full.mime_type").unwrap_or_default();
let media_details_sizes_full_source_url = json_response.string_of("media_details.sizes.full.source_url").unwrap_or_default();
let media_details_image_meta_aperture = json_response.string_of("media_details.image_meta.aperture").unwrap_or_default();
let media_details_image_meta_credit = json_response.string_of("media_details.image_meta.credit").unwrap_or_default();
let media_details_image_meta_camera = json_response.string_of("media_details.image_meta.camera").unwrap_or_default();
let media_details_image_meta_caption = json_response.string_of("media_details.image_meta.caption").unwrap_or_default();
let media_details_image_meta_created_timestamp = json_response.string_of("media_details.image_meta.created_timestamp").unwrap_or_default();
let media_details_image_meta_copyright = json_response.string_of("media_details.image_meta.copyright").unwrap_or_default();
let media_details_image_meta_focal_length = json_response.string_of("media_details.image_meta.focal_length").unwrap_or_default();
let media_details_image_meta_iso = json_response.string_of("media_details.image_meta.iso").unwrap_or_default();
let media_details_image_meta_shutter_speed = json_response.string_of("media_details.image_meta.shutter_speed").unwrap_or_default();
let media_details_image_meta_title = json_response.string_of("media_details.image_meta.title").unwrap_or_default();
let media_details_image_meta_orientation = json_response.string_of("media_details.image_meta.orientation").unwrap_or_default();
let post = json_response.string_of("post").unwrap_or_default();
let source_url = json_response.string_of("source_url").unwrap_or_default();
let mut i = 0;
let mut count_i = json_response.size_of_array("meta");
while i < count_i {
json_response.set_i(i);
i = i + 1;
}
i = 0;
count_i = json_response.size_of_array("media_details.image_meta.keywords");
while i < count_i {
json_response.set_i(i);
i = i + 1;
}
i = 0;
count_i = json_response.size_of_array("missing_image_sizes");
while i < count_i {
json_response.set_i(i);
i = i + 1;
}
i = 0;
count_i = json_response.size_of_array("_links.self");
while i < count_i {
json_response.set_i(i);
let _ = json_response.string_of("_links.self[i].href").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = json_response.size_of_array("_links.collection");
while i < count_i {
json_response.set_i(i);
let _ = json_response.string_of("_links.collection[i].href").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = json_response.size_of_array("_links.about");
while i < count_i {
json_response.set_i(i);
let _ = json_response.string_of("_links.about[i].href").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = json_response.size_of_array("_links.author");
while i < count_i {
json_response.set_i(i);
let _ = json_response.bool_of("_links.author[i].embeddable");
let _ = json_response.string_of("_links.author[i].href").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = json_response.size_of_array("_links.replies");
while i < count_i {
json_response.set_i(i);
let _ = json_response.bool_of("_links.replies[i].embeddable");
let _ = json_response.string_of("_links.replies[i].href").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = json_response.size_of_array("_links.wp:action-unfiltered-html");
while i < count_i {
json_response.set_i(i);
let _ = json_response.string_of("_links.wp:action-unfiltered-html[i].href").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = json_response.size_of_array("_links.wp:action-assign-author");
while i < count_i {
json_response.set_i(i);
let _ = json_response.string_of("_links.wp:action-assign-author[i].href").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = json_response.size_of_array("_links.curies");
while i < count_i {
json_response.set_i(i);
let _ = json_response.string_of("_links.curies[i].name").unwrap_or_default();
let _ = json_response.string_of("_links.curies[i].href").unwrap_or_default();
let _ = json_response.bool_of("_links.curies[i].templated");
i = i + 1;
}