Over the weekend I was trying to see if I can upload a post to WordPress using REST API. While I scoured stack overflow, reddit and other online sites, I was having a hard time understanding how to post. After much trial and error, I came with the following code which works for me.
One big caveat is that you cannot use your WordPress login id and password with REST API. You need to create an user application password for uploading to WordPress using REST API. You can create this user application password by logging into the WordPress, then editing the user that you want to post on behalf of, at the bottom is an application password section in this user profile. The password would be in the format: “pVDQ whdd s1xR o2Uz 0V1K ZbSa”
The code has two functions — one to upload a post and another to upload a file to the media. The primary difference, which I learned the hard way is that these have two different endpoints or URLs.