Sample code for 30+ languages & platforms
React Native

WordPress Media Upload

See more WordPress Examples

Demonstrates how to upload a media file to WordPress.

Chilkat React Native Downloads

React Native
import { DtObj, Http, HttpRequest, HttpResponse, JsonObject } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

  // 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.login = 'wp_username';
  // Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
  http.password = 'app_password';
  http.basicAuth = true;

  const req = new HttpRequest();
  req.httpVerb = '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.contentType = 'image/jpg';
  req.path = '/wp-json/wp/v2/media';

  try {
    req.loadBodyFromFile('qa_data/jpg/starfish.jpg');
  } catch {
    console.log(req.lastErrorText);
    return;
  }

  req.addHeader('Content-Disposition', 'attachment; filename=starfish.jpg');

  req.addHeader('Expect', '100-continue');

  const resp = new HttpResponse();
  try {
    await http.httpSReqAsync('www.yourserver.com', 443, true, req, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  console.log(`HTTP response status: ${resp.statusCode}`);

  const jsonResponse = new JsonObject();
  jsonResponse.load(resp.bodyStr);

  jsonResponse.emitCompact = false;
  console.log(jsonResponse.emit());

  // 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

  const dateGmt = new DtObj();
  let href = '';
  let name = '';

  const id = jsonResponse.intOf('id');
  const date = jsonResponse.stringOf('date');
  jsonResponse.dtOf('date_gmt', false, dateGmt);
  const guidRendered = jsonResponse.stringOf('guid.rendered');
  const guidRaw = jsonResponse.stringOf('guid.raw');
  const modified = jsonResponse.stringOf('modified');
  const modifiedGmt = jsonResponse.stringOf('modified_gmt');
  const slug = jsonResponse.stringOf('slug');
  const status = jsonResponse.stringOf('status');
  const vType = jsonResponse.stringOf('type');
  const link = jsonResponse.stringOf('link');
  const titleRaw = jsonResponse.stringOf('title.raw');
  const titleRendered = jsonResponse.stringOf('title.rendered');
  const author = jsonResponse.intOf('author');
  const commentStatus = jsonResponse.stringOf('comment_status');
  const pingStatus = jsonResponse.stringOf('ping_status');
  const templateArg = jsonResponse.stringOf('template');
  const permalinkTemplate = jsonResponse.stringOf('permalink_template');
  const generatedSlug = jsonResponse.stringOf('generated_slug');
  const descriptionRaw = jsonResponse.stringOf('description.raw');
  const descriptionRendered = jsonResponse.stringOf('description.rendered');
  const captionRaw = jsonResponse.stringOf('caption.raw');
  const captionRendered = jsonResponse.stringOf('caption.rendered');
  const altText = jsonResponse.stringOf('alt_text');
  const mediaType = jsonResponse.stringOf('media_type');
  const mimeType = jsonResponse.stringOf('mime_type');
  const mediaDetailsWidth = jsonResponse.intOf('media_details.width');
  const mediaDetailsHeight = jsonResponse.intOf('media_details.height');
  const mediaDetailsFile = jsonResponse.stringOf('media_details.file');
  const mediaDetailsSizesWoocommerceGalleryThumbnailFile = jsonResponse.stringOf('media_details.sizes.woocommerce_gallery_thumbnail.file');
  const mediaDetailsSizesWoocommerceGalleryThumbnailWidth = jsonResponse.intOf('media_details.sizes.woocommerce_gallery_thumbnail.width');
  const mediaDetailsSizesWoocommerceGalleryThumbnailHeight = jsonResponse.intOf('media_details.sizes.woocommerce_gallery_thumbnail.height');
  const mediaDetailsSizesWoocommerceGalleryThumbnailMimeType = jsonResponse.stringOf('media_details.sizes.woocommerce_gallery_thumbnail.mime_type');
  const mediaDetailsSizesWoocommerceGalleryThumbnailSourceUrl = jsonResponse.stringOf('media_details.sizes.woocommerce_gallery_thumbnail.source_url');
  const mediaDetailsSizesShopThumbnailFile = jsonResponse.stringOf('media_details.sizes.shop_thumbnail.file');
  const mediaDetailsSizesShopThumbnailWidth = jsonResponse.intOf('media_details.sizes.shop_thumbnail.width');
  const mediaDetailsSizesShopThumbnailHeight = jsonResponse.intOf('media_details.sizes.shop_thumbnail.height');
  const mediaDetailsSizesShopThumbnailMimeType = jsonResponse.stringOf('media_details.sizes.shop_thumbnail.mime_type');
  const mediaDetailsSizesShopThumbnailSourceUrl = jsonResponse.stringOf('media_details.sizes.shop_thumbnail.source_url');
  const mediaDetailsSizesFullFile = jsonResponse.stringOf('media_details.sizes.full.file');
  const mediaDetailsSizesFullWidth = jsonResponse.intOf('media_details.sizes.full.width');
  const mediaDetailsSizesFullHeight = jsonResponse.intOf('media_details.sizes.full.height');
  const mediaDetailsSizesFullMimeType = jsonResponse.stringOf('media_details.sizes.full.mime_type');
  const mediaDetailsSizesFullSourceUrl = jsonResponse.stringOf('media_details.sizes.full.source_url');
  const mediaDetailsImageMetaAperture = jsonResponse.stringOf('media_details.image_meta.aperture');
  const mediaDetailsImageMetaCredit = jsonResponse.stringOf('media_details.image_meta.credit');
  const mediaDetailsImageMetaCamera = jsonResponse.stringOf('media_details.image_meta.camera');
  const mediaDetailsImageMetaCaption = jsonResponse.stringOf('media_details.image_meta.caption');
  const mediaDetailsImageMetaCreatedTimestamp = jsonResponse.stringOf('media_details.image_meta.created_timestamp');
  const mediaDetailsImageMetaCopyright = jsonResponse.stringOf('media_details.image_meta.copyright');
  const mediaDetailsImageMetaFocalLength = jsonResponse.stringOf('media_details.image_meta.focal_length');
  const mediaDetailsImageMetaIso = jsonResponse.stringOf('media_details.image_meta.iso');
  const mediaDetailsImageMetaShutterSpeed = jsonResponse.stringOf('media_details.image_meta.shutter_speed');
  const mediaDetailsImageMetaTitle = jsonResponse.stringOf('media_details.image_meta.title');
  const mediaDetailsImageMetaOrientation = jsonResponse.stringOf('media_details.image_meta.orientation');
  const post = jsonResponse.stringOf('post');
  const sourceUrl = jsonResponse.stringOf('source_url');
  let i = 0;
  let countI = jsonResponse.sizeOfArray('meta');
  while (i < countI) {
    jsonResponse.i = i;
    i++;
  }

  i = 0;
  countI = jsonResponse.sizeOfArray('media_details.image_meta.keywords');
  while (i < countI) {
    jsonResponse.i = i;
    i++;
  }

  i = 0;
  countI = jsonResponse.sizeOfArray('missing_image_sizes');
  while (i < countI) {
    jsonResponse.i = i;
    i++;
  }

  i = 0;
  countI = jsonResponse.sizeOfArray('_links.self');
  while (i < countI) {
    jsonResponse.i = i;
    href = jsonResponse.stringOf('_links.self[i].href');
    i++;
  }

  i = 0;
  countI = jsonResponse.sizeOfArray('_links.collection');
  while (i < countI) {
    jsonResponse.i = i;
    href = jsonResponse.stringOf('_links.collection[i].href');
    i++;
  }

  i = 0;
  countI = jsonResponse.sizeOfArray('_links.about');
  while (i < countI) {
    jsonResponse.i = i;
    href = jsonResponse.stringOf('_links.about[i].href');
    i++;
  }

  i = 0;
  countI = jsonResponse.sizeOfArray('_links.author');
  while (i < countI) {
    jsonResponse.i = i;
    jsonResponse.boolOf('_links.author[i].embeddable');
    href = jsonResponse.stringOf('_links.author[i].href');
    i++;
  }

  i = 0;
  countI = jsonResponse.sizeOfArray('_links.replies');
  while (i < countI) {
    jsonResponse.i = i;
    jsonResponse.boolOf('_links.replies[i].embeddable');
    href = jsonResponse.stringOf('_links.replies[i].href');
    i++;
  }

  i = 0;
  countI = jsonResponse.sizeOfArray('_links.wp:action-unfiltered-html');
  while (i < countI) {
    jsonResponse.i = i;
    href = jsonResponse.stringOf('_links.wp:action-unfiltered-html[i].href');
    i++;
  }

  i = 0;
  countI = jsonResponse.sizeOfArray('_links.wp:action-assign-author');
  while (i < countI) {
    jsonResponse.i = i;
    href = jsonResponse.stringOf('_links.wp:action-assign-author[i].href');
    i++;
  }

  i = 0;
  countI = jsonResponse.sizeOfArray('_links.curies');
  while (i < countI) {
    jsonResponse.i = i;
    name = jsonResponse.stringOf('_links.curies[i].name');
    href = jsonResponse.stringOf('_links.curies[i].href');
    jsonResponse.boolOf('_links.curies[i].templated');
    i++;
  }
}