How to create WordPress blog posts from C# with HttpClient and the WordPress REST API on Docker for Windows 10




Geniuswp show

Summary: To create WordPress blog posts from C# I will be using docker container on my Windows 10 Pro installation.Before installing docker on Windows 10 make sure the Windows 10 feature “Hyper-V” is enabled:I followed this blog post to install docker on Windows 10https://docs.docker.com/docker-for-windows/install/#about-windows-containersTo create a WordPress docker container I followed this post:https://docs.docker.com/compose/wordpress/When the WordPress docker container is running, you should create your first blog post manually on http://localhost:8000.Now reading all blog post data, is easy, because we can use an GET request without authentication, by using the C# code below.Now when you want to create a blog post from C# you will need to use a POST request with authentication.I used JWT (JSON Web Tokens).When you want to use JWT in WordPress you should first install the JWT Authentication for WP-API plugin. After installing the plugin we must first edit two files before activating the plugin.Edit .htaccess file on WindowsGet WordPress container name on windows:Open PowerShell and enter:docker ps –allCopy .htaccess file from docker container to local Windows host:docker cp wordpress_wordpress_1:/var/www/html/.htaccess .htaccessEdit the file with your preferred editor (I used Visual Studio Code), add the rows in red:# BEGIN WordPressRewriteEngine OnRewriteBase /RewriteRule ^index\.php$ – [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]RewriteCond %{HTTP:Authorization} ^(.*) RewriteRule ^(.*) – [E=HTTP_AUTHORIZATION:%1] SetEnvIf Authorization “(.*)” HTTP_AUTHORIZATION=$1# END WordPressCopy .htaccess file from local Windows host to Docker container:docker cp .htaccess wordpress_wordpress_1:/var/www/html/.htaccess Edit wp-config.php on WindowsGet WordPress container name on windows:Open PowerShell and enter:docker ps –allCopy wp-config.php file from docker container to local Windows host:docker cp wordpress_wordpress_1:/var/www/html/wp-config.php wp-config.phpEdit the file with your preferred editor (I used Visual Studio Code), add the rows in blue: