Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to the JSON REST API

Introduction to the JSON REST API

Rachel Baker

October 08, 2014
Tweet

More Decks by Rachel Baker

Other Decks in Technology

Transcript

  1. JSON { "ID": 4, "title": "My Awesome Post", "content": "This

    is my awesome content!" } JavaScript Object Notation • Supported by basically everything • Human readable • Standard
  2. REST Actions applied to Resources Create POST Read GET Update

    PUT Delete DELETE Uses HTTP actions: Representational State Transfer
  3. REST Representational State Transfer /posts /posts/4 /posts /posts/4 /posts/4 GET

    GET POST PUT DELETE Action
 (Verb) Resource
 (Object)
  4. API Or extend as needed! GET POST PUT DELETE Posts

    ✔ ✔ ✔ ✔ Pages ✔ ✔ ✔ ✔ Custom Post Types ✔ ✔ ✔ ✔ Post Meta ✔ ✔ ✔ ✔ Media ✔ ✔ ✔ ✔ Comments ✔ ✔ Taxonomies/Terms ✔ ✔ ✔ ✔ Users ✔ ✔ ✔ ✔
  5. WordPress XML-RPC API Turned on by default since WordPress 3.5

    Flexible and extendible Works well with documents and media
 Insecure authentication Not suited to exchanging data Large amount of code required to parse XML Cannot control access to certain apps
  6. Jetpack API Easy for developers Works with Javascript easily (JSON-based)


    Requires Jetpack active Depends on WordPress.com Does not work for local development Cannot control access to certain apps
  7. { "name": "WP API Dev", "description": "Just another WordPress site",

    "URL": "http://local.wpapi.dev", "routes": {}, "authentication": [], "meta": {} } /wp-json/ GET
  8. [{ "ID": 110, "title": "Testing sticky", "status": "publish", "type": "post",

    "author": {}, "content": "<p>this should be stuck as a post. Stuck on you.</p>\n", "link": "http://local.wpapi.dev/testing-sticky/", "date": "2014-06-02T10:47:39-05:00", "modified": "2014-06-02T10:47:39-05:00", "format": "standard", "slug": "testing-sticky-2", "guid": "http://local.wpapi.dev/testing-sticky-2/", "excerpt": "<p>this should be stuck as a post. Stuck on you.</p>\n”, "comment_status": "open", "sticky": true, "meta": {}, "featured_image": null, "terms": {} }] /wp-json/posts GET
  9. [{ "name": "Categories", "slug": "category", "labels": {}, "types": {}, "show_cloud":

    true, "hierarchical": true, "meta": {} },{ "name": "Tags", "slug": "post_tag", "labels": {}, "types": {}, "show_cloud": true, "hierarchical": false, "meta": {} },] /wp-json/taxonomies GET
  10. [{ "ID": 121, "title": "puppies", "type": "attachment", "author": {}, "content":

    "<p class=\"attachment\"><a href='http://local.wpapi.dev/wp- content/uploads/2014/07/puppies.jpeg'><img width=\"300\" height=\"225\" src=\"http://local.wpapi.dev/wp-content/uploads/2014/07/ puppies-300x225.jpeg\" class=\"attachment-medium\" alt=\"puppies\" /></ a></p>\n", "link": "http://local.wpapi.dev/?attachment_id=121", "date": "2014-07-25T17:07:02-05:00", "modified": "2014-07-25T17:07:02-05:00", "slug": "puppies", "guid": "http://local.wpapi.dev/wp-content/uploads/2014/07/puppies.jpeg", "meta": {}, "terms": [], "attachment_meta": { "width": 1600, "height": 1200, "file": "2014/07/puppies.jpeg", "sizes": {}, "image_meta": {} }] /wp-json/media GET
  11. { "ID": 1, "title": "Hello World!", "status": "publish", "type": "post",

    "author": {}, "content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>\n", "link": "http://local.wpapi.dev/hello-world/", "date": "2014-04-18T15:14:46-05:00", "modified": "2014-05-02T01:26:30-05:00", "format": "standard", "slug": "hello-world", "guid": "http://local.wpapi.dev/?p=1", "excerpt": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>\n", "comment_status": "open", "meta": {}, "featured_image": null, "terms": {} } /wp-json/posts/1 GET
  12. [{ "ID": 1, "post": 1, "content": "<p>Hi, this is a

    comment.<br />\nTo delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.</p>\n", "status": "approved", "type": "comment", "author": { "ID": 0, "name": "Mr WordPress", "URL": "https://wordpress.org/", "avatar": "http://0.gravatar.com/avatar/ ad516503a11cd5ca435acc9bb6523536?s=96" }, "date": "2014-04-18T15:14:46-05:00", "meta": { "links": { "up": "http://local.wpapi.dev/wp-json/posts/1", "self": "http://local.wpapi.dev/wp-json/posts/1/comments/1" }}}] /wp-json/posts/1/comments GET
  13. [{ "code": "json_user_cannot_list", "message": "Sorry, you are not allowed to

    list users." }] /wp-json/users GET GET /wp-json/posts/1/meta [{ "code": "json_cannot_edit", "message": "Sorry, you cannot edit this post” }]
  14. /wp-json/posts POST curl -X POST -H "Content-Type: application/json" -d '!

    {! ! "title":"Put Your Content to REST with WP API",! ! "content_raw":"<p>The <a href=\"https:\/\/github.com\/WP-API\/WP-API\" title=\"WordPress JSON REST API\" target=\"_blank\">JSON REST API (WP API)<\/a> is coming to WordPress Core in WordPress 4.1. Come to learn more what the JSON REST API is, why this upcoming feature is a <strong>game changer<\/strong> for WordPress as an <em>application platform<\/em>, and leave with some examples of how you can be using or extending it today.<\/p><p>We will walk through examples of how to make HTTP requests to view resources through the API, and <a href= \"https:\/\/github.com\/WP-API\/WP-API\/blob\/master\/docs\/authentication.md\" target=\"_blank\">authenticating<\/a> to perform more sensitive actions such as creating, editing, and deleting resources.<\/p>”,! ! “excerpt_raw": "Come to learn more what the JSON REST API is, why this upcoming feature is a game changer for WordPress as an application platform, and leave with some examples of how you can be using or extending it today.”,! ! "status": "publish",! ! "sticky": true,! ! "post_meta" : [! ! ! {! ! ! ! "key" : "speaker_name",! ! ! ! "value" : "Rachel Baker"! ! ! },! ! ! {! ! ! ! "key" : "speaker_twitter",! ! ! ! "value" : "\u0040rachelbaker"! ! ! }! ! ]! }! ' -u admin:password http://wcchi.dev/wp-json/posts/! Request Basic Auth
  15. Response {! ! "ID":6,! ! "title":"Put Your Content to REST

    with WP API",! ! "status":"publish",! ! "type":"post",! ! "author":{! ! ! "ID":1,! ! ! "username":"admin",! ! ! "name":"admin",! ! ! "first_name":"",! ! ! "last_name":"",! ! ! "nickname":"admin",! ! ! "slug":"admin",! ! ! "URL":"",! ! ! "avatar":"http:\/\/1.gravatar.com\/avatar\/7c4ff521986b4ff8d29440beec01972d?s=96",! ! ! "description":"",! ! ! "email":"[email protected]",! ! ! "registered":"2014-06-05T14:39:17+00:00",! ! ! "meta":{! ! ! ! "links":{! ! ! ! ! "self":"http:\/\/wcchi.dev\/wp-json\/users\/1",! ! ! ! ! "archives":"http:\/\/wcchi.dev\/wp-json\/users\/1\/posts"! ! ! ! }! ! ! }! ! },! ! "content":"<p>The <a href=\"https:\/\/github.com\/WP-API\/WP-API\" title=\"WordPress JSON REST API\" target=\"_blank\">JSON REST API (WP API)< \/a> is coming to WordPress Core in WordPress 4.1. Come to learn more what the JSON REST API is, why this upcoming feature is a <strong>game changer<\/strong> for WordPress as an <em>application platform<\/em>, and leave with some examples of how you can be using or extending it today.<\/ p>\n<p>We will walk through examples of how to make HTTP requests to view resources through the API, and <a href=\"https:\/\/github.com\/WP-API\/WP- API\/blob\/master\/docs\/authentication.md\" target=\"_blank\">authenticating<\/a> to perform more sensitive actions such as creating, editing, and deleting resources.<\/p>\n",! ! "parent":0,! ! "link":"http:\/\/wcchi.dev\/put-your-content-to-rest-with-wp-api\/",! ! "date":"2014-06-13T11:39:55+05:00",! ! "modified":"2014-06-13T11:39:55+05:00",! ! "format":"standard",! ! "slug":"put-your-content-to-rest-with-wp-api",! ! "guid":"http:\/\/wcchi.dev\/put-your-content-to-rest-with-wp-api\/",! ! "excerpt":"<p>The JSON REST API (WP API) is coming to WordPress Core in WordPress 4.1. Come to learn more what the JSON REST API is,! ! why this upcoming feature is a game changer for WordPress as an application platform,! ! and leave with some examples of how you can be using or extending it today. We will [&hellip;]<\/p>\n",! ! "menu_order":0,! ! "comment_status":"open",! ! "ping_status":"open",! ! "sticky":true,! ! "date_tz":"Etc\/GMT-5",! ! "date_gmt":"2014-06-13T16:39:55+00:00",! ! "modified_tz":"Etc\/GMT-5",! ! "modified_gmt":"2014-06-13T16:39:55+00:00",! ! "password":"",! ! "meta":{! ! ! "links":{! ! ! ! "self":"http:\/\/wcchi.dev\/wp-json\/posts\/6",! ! ! ! "author":"http:\/\/wcchi.dev\/wp-json\/users\/1",! ! ! ! "collection":"http:\/\/wcchi.dev\/wp-json\/posts",! ! ! ! "replies":"http:\/\/wcchi.dev\/wp-json\/posts\/6\/comments",! ! ! ! "version-history":"http:\/\/wcchi.dev\/wp-json\/posts\/6\/revisions"! ! ! }!
  16. /wp-json/users/1 PUT Request Request curl -X PUT -H "Content-Type: application/json"

    -d '! {! ! "name":"Rachel Baker",! ! "first_name":"Rachel",! ! "last_name": “Baker",! ! "nickname": “rachelbaker",! ! "email": “rachel\u0040rachelbaker.me"! }! ' -u admin:password http://wcchi.dev/wp-json/users/1/!
  17. Response {! ! "ID":1,! ! "username":"admin",! ! "name":"Rachel Baker",! !

    "first_name":"Rachel",! ! "last_name":"Baker",! ! "nickname":"rachelbaker",! ! "slug":"admin",! ! "URL":"",! ! "avatar":"http:\/\/0.gravatar.com\/avatar\/634b37a53babc18a5bda19722d5b41a3?s=96",! ! "description":"",! ! "registered":"2014-06-05T14:39:17+00:00",! ! "roles":["administrator"],! ! “capabilities":{! ! ! "switch_themes":true,! ! "edit_themes":true,! ! "activate_plugins":true,! ! "edit_plugins":true,! ! "edit_users":true,! ! "edit_files":true,! ! "manage_options":true,! ! "moderate_comments":true,! ! "manage_categories":true,! ! "manage_links":true,! ! "upload_files":true,! ! "import":true,! ! "unfiltered_html":true,! ! "edit_posts":true,! ! "edit_others_posts":true,! ! "edit_published_posts":true,! ! "publish_posts":true,! ! "edit_pages":true,! ! "read":true,! ! "level_10":true,! ! "level_9":true,! ! "level_8":true,! ! "level_7":true,! ! "level_6":true,! ! "level_5":true,! ! "level_4":true,! ! "level_3":true,! ! "level_2":true,! ! "level_1":true,! ! "level_0":true,! ! "edit_others_pages":true,! ! "edit_published_pages":true,! ! "publish_pages":true,! ! "delete_pages":true,! ! "delete_others_pages":true,! ! "delete_published_pages":true,! ! "delete_posts":true,! ! "delete_others_posts":true,! ! "delete_published_posts":true,! ! "delete_private_posts":true,! ! ! "edit_private_posts":true,!
  18. Use Cases: Perform CRUD operations 
 on the client-side Backbone.js

    themes or plugins ! Receiving and populating content 
 for “infinite scrolling” 
 Form validation and submissions
 
 Front-end content editing

  19. Use Cases: Perform CRUD operations 
 on another WordPress site

    ! Display content from a separate WordPress site ! Display content from another site 
 within a multisite network ! Manage (create, update, delete) content on 
 a separate WordPress site or another site 
 within a multisite network !
  20. Use Cases: Break your content free 
 from WordPress !

    Display and manage your content in 
 a mobile application ! Integrate your content with other 
 application platforms ! Manage (create, update, delete) content on 
 a separate WordPress site or another site 
 within a multisite network
  21. Current Examples: ! New York Times ! Wired ! My

    Blog ! 10up Bocoup and more! ! Folks already using the JSON REST API
  22. Resources: JSON REST API JSON REST API Plugin https://github.com/WP-API/WP-API or

    https://wordpress.org/plugins/json-rest-api/ ! JSON REST API Auth Plugins and Clients https://github.com/WP-API ! !