Sample code for 30+ languages & platforms
Unicode C++

WordPress Authentication with Applications Password Plugin

See more WordPress Examples

Demonstrates basic username/password authentication using the WordPress Applications Password plugin.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkStringBuilderW.h>
#include <CkJsonArrayW.h>
#include <CkDtObjW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  This example assumes the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    CkHttpW http;

    //  Implements the following CURL command:

    //  curl -X GET --user wp_username:app_password https://www.yoursite.com/wp-json/wp/v2/posts?page=1

    //  Use the following online tool to generate HTTP code from a CURL command
    //  Convert a cURL Command to HTTP Source Code

    //  Use your WordPress login, such as "admin", not the application name.
    http.put_Login(L"wp_username");
    //  Use the application password, such as "Nths RwVH eDJ4 weNZ orMN jabq"
    http.put_Password(L"app_password");
    http.put_BasicAuth(true);

    CkStringBuilderW sbResponseBody;
    success = http.QuickGetSb(L"https://www.yoursite.com/wp-json/wp/v2/posts?page=1",sbResponseBody);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    CkJsonArrayW jarrResp;
    jarrResp.LoadSb(sbResponseBody);
    jarrResp.put_EmitCompact(false);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",jarrResp.emit());

    int respStatusCode = http.get_LastStatus();
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",http.lastHeader());
        wprintf(L"Failed.\n");
        return;
    }

    //  Sample JSON response:
    //  (Sample code for parsing the JSON response is shown below)

    //  [
    //    {
    //      "id": 1902,
    //      "date": "2020-11-16T09:54:09",
    //      "date_gmt": "2020-11-16T16:54:09",
    //      "guid": {
    //        "rendered": "http:\/\/cknotes.com\/?p=1902"
    //      },
    //      "modified": "2020-11-16T09:54:09",
    //      "modified_gmt": "2020-11-16T16:54:09",
    //      "slug": "xero-redirect-uri-for-oauth2-and-desktop-apps",
    //      "status": "publish",
    //      "type": "post",
    //      "link": "https:\/\/cknotes.com\/xero-redirect-uri-for-oauth2-and-desktop-apps\/",
    //      "title": {
    //        "rendered": "Xero Redirect URI for OAuth2 and Desktop Apps"
    //      },
    //      "content": {
    //        "rendered": "<p>...",
    //        "protected": false
    //      },
    //      "excerpt": {
    //        "rendered": "<p>...",
    //        "protected": false
    //      },
    //      "author": 1,
    //      "featured_media": 0,
    //      "comment_status": "closed",
    //      "ping_status": "open",
    //      "sticky": false,
    //      "template": "",
    //      "format": "standard",
    //      "meta": [
    //      ],
    //      "categories": [
    //        815
    //      ],
    //      "tags": [
    //        594,
    //        816
    //      ],
    //      "_links": {
    //        "self": [
    //          {
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902"
    //          }
    //        ],
    //        "collection": [
    //          {
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"
    //          }
    //        ],
    //        "about": [
    //          {
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"
    //          }
    //        ],
    //        "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=1902"
    //          }
    //        ],
    //        "version-history": [
    //          {
    //            "count": 1,
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902\/revisions"
    //          }
    //        ],
    //        "predecessor-version": [
    //          {
    //            "id": 1904,
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902\/revisions\/1904"
    //          }
    //        ],
    //        "wp:attachment": [
    //          {
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=1902"
    //          }
    //        ],
    //        "wp:term": [
    //          {
    //            "taxonomy": "category",
    //            "embeddable": true,
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=1902"
    //          },
    //          {
    //            "taxonomy": "post_tag",
    //            "embeddable": true,
    //            "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=1902"
    //          }
    //        ],
    //        "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

    //  Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    //  See this example explaining how this memory should be used: const char * functions.

    CkDtObjW date_gmt;
    CkJsonObjectW *json = 0;
    int id;
    const wchar_t *date = 0;
    const wchar_t *guidRendered = 0;
    const wchar_t *modified = 0;
    const wchar_t *modified_gmt = 0;
    const wchar_t *slug = 0;
    const wchar_t *status = 0;
    const wchar_t *v_type = 0;
    const wchar_t *link = 0;
    const wchar_t *titleRendered = 0;
    const wchar_t *contentRendered = 0;
    bool contentProtected;
    const wchar_t *excerptRendered = 0;
    bool excerptProtected;
    int author;
    int featured_media;
    const wchar_t *comment_status = 0;
    const wchar_t *ping_status = 0;
    bool sticky;
    const wchar_t *template = 0;
    const wchar_t *format = 0;
    int j;
    int count_j;
    int intVal;
    const wchar_t *href = 0;
    bool embeddable;
    int count;
    const wchar_t *taxonomy = 0;
    const wchar_t *name = 0;
    bool templated;

    int i = 0;
    int count_i = jarrResp.get_Size();
    while (i < count_i) {
        json = jarrResp.ObjectAt(i);
        id = json->IntOf(L"id");
        date = json->stringOf(L"date");
        json->DtOf(L"date_gmt",false,date_gmt);
        guidRendered = json->stringOf(L"guid.rendered");
        modified = json->stringOf(L"modified");
        modified_gmt = json->stringOf(L"modified_gmt");
        slug = json->stringOf(L"slug");
        status = json->stringOf(L"status");
        v_type = json->stringOf(L"type");
        link = json->stringOf(L"link");
        titleRendered = json->stringOf(L"title.rendered");
        contentRendered = json->stringOf(L"content.rendered");
        contentProtected = json->BoolOf(L"content.protected");
        excerptRendered = json->stringOf(L"excerpt.rendered");
        excerptProtected = json->BoolOf(L"excerpt.protected");
        author = json->IntOf(L"author");
        featured_media = json->IntOf(L"featured_media");
        comment_status = json->stringOf(L"comment_status");
        ping_status = json->stringOf(L"ping_status");
        sticky = json->BoolOf(L"sticky");
        template = json->stringOf(L"template");
        format = json->stringOf(L"format");
        j = 0;
        count_j = json->SizeOfArray(L"meta");
        while (j < count_j) {
            json->put_J(j);
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"categories");
        while (j < count_j) {
            json->put_J(j);
            intVal = json->IntOf(L"categories[j]");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"tags");
        while (j < count_j) {
            json->put_J(j);
            intVal = json->IntOf(L"tags[j]");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"_links.self");
        while (j < count_j) {
            json->put_J(j);
            href = json->stringOf(L"_links.self[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"_links.collection");
        while (j < count_j) {
            json->put_J(j);
            href = json->stringOf(L"_links.collection[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"_links.about");
        while (j < count_j) {
            json->put_J(j);
            href = json->stringOf(L"_links.about[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"_links.author");
        while (j < count_j) {
            json->put_J(j);
            embeddable = json->BoolOf(L"_links.author[j].embeddable");
            href = json->stringOf(L"_links.author[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"_links.replies");
        while (j < count_j) {
            json->put_J(j);
            embeddable = json->BoolOf(L"_links.replies[j].embeddable");
            href = json->stringOf(L"_links.replies[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"_links.version-history");
        while (j < count_j) {
            json->put_J(j);
            count = json->IntOf(L"_links.version-history[j].count");
            href = json->stringOf(L"_links.version-history[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"_links.predecessor-version");
        while (j < count_j) {
            json->put_J(j);
            id = json->IntOf(L"_links.predecessor-version[j].id");
            href = json->stringOf(L"_links.predecessor-version[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"_links.wp:attachment");
        while (j < count_j) {
            json->put_J(j);
            href = json->stringOf(L"_links.wp:attachment[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"_links.wp:term");
        while (j < count_j) {
            json->put_J(j);
            taxonomy = json->stringOf(L"_links.wp:term[j].taxonomy");
            embeddable = json->BoolOf(L"_links.wp:term[j].embeddable");
            href = json->stringOf(L"_links.wp:term[j].href");
            j = j + 1;
        }

        j = 0;
        count_j = json->SizeOfArray(L"_links.curies");
        while (j < count_j) {
            json->put_J(j);
            name = json->stringOf(L"_links.curies[j].name");
            href = json->stringOf(L"_links.curies[j].href");
            templated = json->BoolOf(L"_links.curies[j].templated");
            j = j + 1;
        }

        delete json;
        i = i + 1;
    }
    }