WP- REST-API with Python

API endpoint

import http.client
 
def main():
    conn = http.client.HTTPConnection("wp-api.pw")
    
    conn.request("GET", "/wp-json/")
    res = conn.getresponse()
    print(res.status, res.reason)
    print(res.read())
    conn.close()
 
if __name__ == "__main__":
    main()

Posts

Get

Post

Delete

Media

Post

Users

Get

WP- REST-API with cURL

API endpoint

[chen@luna ~]$ curl -l http://wp-api.pw/wp-json/

Posts

Get

The Posts endpoint returns a Post Collection containing a subset of the site’s posts.

[chen@luna ~]$ curl -l http://wp-api.pw/wp-json/posts

Post

Create a Post Requires authentication

[chen@luna ~]$ cat data.json
{
    "title": "This is a post",
    "content_raw": "This is some content"
}
[chen@luna ~]$ curl --user test:password -X POST http://wp-api.pw/wp-json/posts --data @data.json

Delete

Delete a Post Requires authentication

[chen@luna ~]$ curl --user test:password -X DELETE http://wp-api.pw/wp-json/posts/229
{"message":"Deleted post"}

Media

Post

To create the raw data for an attachment. This is a binary object (blob), such as image data or a video.

[chen@luna ~]$ curl --user test:password -H 'Content-Type:image/jpeg' -H 'Content-Disposition: attachment; filename="test1551.jpg"' -X POST http://wp-api.pw/wp-json/media --data-binary @/home/chen/IMG_1551.jpg

Users

Get

This endpoint offers a permalink to get the current user, without needing to know the user’s ID.

get user self

[chen@luna ~]$ curl --user test:password http://wp-api.pw/wp-json/users/me

get user list

[chen@luna ~]$ curl --user test:password http://wp-api.pw/wp-json/users

WP- REST-API with Java

API endpoint

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
 
 
public class Main {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://wp-api.pw/wp-json/");
 
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
 
            System.out.printf("Response: %d %sn", conn.getResponseCode(), conn.getResponseMessage());
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String input;
 
            while ((input = br.readLine()) != null) {
                System.out.println(input);
            }
            br.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Posts

Get

The Posts endpoint returns a Post Collection containing a subset of the site’s posts.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
 
 
public class Main {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://wp-api.pw/wp-json/posts");
 
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
 
            System.out.printf("Response: %d %sn", conn.getResponseCode(), conn.getResponseMessage());
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String input;
 
            while ((input = br.readLine()) != null) {
                System.out.println(input);
            }
            br.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Post

Create a Post Requires authentication

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
 
 
public class Main {
    public static void main(String[] args) {
        try {
            String content = '{"title": "This is a post","content_raw": "This is some content"}';
            URL url = new URL("http://wp-api.pw/wp-json/posts");
 
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("test:password".getBytes(StandardCharsets.UTF_8)));
            conn.setRequestProperty("Content-Length", String.valueOf(content.getBytes("UTF-8").length));
            conn.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.writeBytes(content);
            wr.flush();
            wr.close();
 
            System.out.printf("Response: %d %sn", conn.getResponseCode(), conn.getResponseMessage());
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String input;
 
            while ((input = br.readLine()) != null) {
                System.out.println(input);
            }
            br.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Delete

Delete a Post Requires authentication

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
 
 
public class Main {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://wp-api.pw/wp-json/posts/229");
 
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("DELETE");
            conn.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("test:password".getBytes(StandardCharsets.UTF_8)));
 
            System.out.printf("Response: %d %sn", conn.getResponseCode(), conn.getResponseMessage());
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String input;
 
            while ((input = br.readLine()) != null) {
                System.out.println(input);
            }
            br.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Media

Post

To create the raw data for an attachment. This is a binary object (blob), such as image data or a video.

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
 
 
public class Main {
    public static void main(String[] args) {
        try {
            char[] buffer = new char[1024];
            StringWriter writer = new StringWriter();
            FileReader fileReader = new FileReader("/home/chen/IMG_1551.jpg");
            for (int n = 0; -1 != (n = fileReader.read(buffer));) {
                writer.write(buffer, 0, n);
            }
            fileReader.close();
            String content = writer.toString();
            URL url = new URL("http://wp-api.pw/wp-json/media");
 
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "image/jpeg");
            conn.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("test:password".getBytes(StandardCharsets.UTF_8)));
            conn.setRequestProperty("Content-Length", String.valueOf(content.getBytes("UTF-8").length));
            conn.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.writeBytes(content);
            wr.flush();
            wr.close();
 
            System.out.printf("Response: %d %sn", conn.getResponseCode(), conn.getResponseMessage());
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String input;
 
            while ((input = br.readLine()) != null) {
                System.out.println(input);
            }
            br.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Users

Get

This endpoint offers a permalink to get the current user, without needing to know the user’s ID.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
 
 
public class Main {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://wp-api.pw/wp-json/users/me");
 
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("test:password".getBytes(StandardCharsets.UTF_8)));
 
            System.out.printf("Response: %d %sn", conn.getResponseCode(), conn.getResponseMessage());
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String input;
 
            while ((input = br.readLine()) != null) {
                System.out.println(input);
            }
            br.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

App Inventor 2が今年末にリリース

App Inventor 2が今年末にリリースされます。
ブロックエディタも含め、完全にブラウザで動作可能で、JAVAの起動が不要です。
詳細は、以下のアナウンスをご確認下さい。
(私は、要点だけ理解しましたが、どなたか翻訳して頂けると助かります。)
現在、App Inventor 2 Alphaは、下記URLから利用可能です。
(Alpha版の注意点が、ありますので、上記アナウンスをよく読んでください。)

“*nothing*”が返ってくる致命的なエラー

AppInventor API のプラグインを作って、WordPress.Orgで公開した。

テストの段階でわかったことだが、Tagに対して、Valueがないので場合、NULLが帰って来る。

このNULLは、AppInventorでは、”*nothing*”と表現し、致命的なエラーとなり、ダイアログを出して異常終了するしか救いようがない!

ネットでいろいろ調べたけど、”*nothing*”は、NULLではなく、未定義にとするらしい。発生させないように回避しか対策がない。

こまった。

AppInventor API変更するしかないかな。