| Under Development The following examples all assume that the authorization infrastructure is not in place for the OpenPTK Server. This is under development and will impact the specific commands below as several of these examples will require a valid logged in session and the privledges to perform the operations listed below. The Server creates an anonymous session if no login or existing session information is supplied. |
Introduction
This page demonstrates several examples of using the OpenPTK RESTful interface with the cURL command line utility. The examples below show how to use the HTTP Accept header to request each of the 4 representation outputs supported (JSON, XML, HTML, and text).
Engine Resources
curl -H "Accept: text/plain" http://localhost:8080/openptk/resources/engine/ curl -H "Accept: text/plain" http://localhost:8080/openptk/resources/engine/sessions/ curl -H "Accept: text/plain" http://localhost:8080/openptk/resources/engine/sessions/<session>
Context Resources
JSON:
curl -H "Accept: application/json" http://localhost:8080/openptk/resources/contexts/ curl -H "Accept: application/json" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC curl -H "Accept: application/json" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBCBC/subjects/ curl -H "Accept: application/json" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/?search=John curl -H "Accept: application/json" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324
XML:
curl -H "Accept: application/xml" http://localhost:8080/openptk/resources/contexts/ curl -H "Accept: application/xml" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC curl -H "Accept: application/xml" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ curl -H "Accept: application/xml" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/?search=John curl -H "Accept: application/xml" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324
HTML:
curl -H "Accept: text/html" http://localhost:8080/openptk/resources/contexts/ curl -H "Accept: text/html" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC curl -H "Accept: text/html" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ curl -H "Accept: text/html" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/?search=John curl -H "Accept: text/html" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324
Plain Text:
curl -H "Accept: text/plain" http://localhost:8080/openptk/resources/contexts/ curl -H "Accept: text/plain" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC curl -H "Accept: text/plain" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ curl -H "Accept: text/plain" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/?search=John curl -H "Accept: text/plain" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324
Create, Update and Delete Examples
Create a Subject:
curl -X POST -v -H "Content-Type: application/json" -d '{"subject" : { "attributes" : { "lastname" : "User", "title" : "REST User", "firstname" : "Curl", "telephone" : "123-456-7890", "email" : "curl@openptk.org" }}}' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects curl -X POST -v -H "Content-Type: application/xml" -d '<subject><attributes><lastname type="string">User</lastname><title type="string">REST User</title><firstname type="string">Curl</firstname><telephone type="string">123-456-7890</telephone><email type="string">curl@openptk.org</email></attributes></subject>' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects
Change attributes of a Subject:
CHANGE TITLE AND EMAIL curl -X PUT -v -H "Content-Type: application/json" -d '{ "subject" : { "attributes" : { "title" : "Jersey (JSR-311) Expert", "email" : "restful@openptk.org" } } }' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324 curl -X PUT -v -H "Content-Type: application/xml" -d '<subject><attributes><title type="string">Jersey (JSR-311) Expert</title><email type="string">restful@openptk.org</email></attributes></subject>' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324 CHANGE FORGOTTEN PASSWORD QUESTIONS AND ANSWERS curl -X PUT -v -H "Content-Type: application/json" -d '{ "subject" : { "attributes" : { "forgottenPasswordAnswers" : [ "Smith","Chicago","1234" ], "forgottenPasswordQuestions" : [ "Mothers Maiden Name","City you were born","Last 4 digits of Frequent Flyer" ] } } }' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324 curl -X PUT -v -H "Content-Type: application/xml" -d '<subject><attributes><forgottenPasswordQuestions type="string"><values><value>Mothers Maiden Name</value><value>City you were born</value><value>Last 4 digits of Frequent Flyer</value></values></forgottenPasswordQuestions><forgottenPasswordAnswers type="string"><values><value>Smith</value><value>Chicago</value><value>1234</value></values></forgottenPasswordAnswers></attributes></subject>' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324
Delete a Subject:
curl -X DELETE -v http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/cuser
Password Management
Reset
curl -X GET -v -H "Accept: application/json" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324/password/reset
Change
curl -X PUT -v -H "Content-Type: application/json" -d '{ "subject" : { "attributes" : { "password" : "Passw0rd" }}}' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324/password/change curl -X PUT -v -H "Content-Type: application/xml" -d '<subject><attributes><password type="string">Passw0rd</password></attributes></subject>' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324/password/change
Get Forgot Password Questions (Phase 1)
Note: Answering the forgotten password questions is a 2 phase operation and requires the same anonymous session ID to be used in order to validate the answers after the answers are returned. This is not true stateless REST design, but in order to accomodate dynamic questions and a subset of the available questions to be answered, the supplied questions (phase 1) are tied to the answered questions (phase 2) by using a session variable on the anonymous session. For this reason the cookie which tracks the session must be supplied in the phase 2 answers.
curl -c cookies.txt -v -H "Accept: application/json" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324/password/forgot/questions
curl -c cookies.txt -v -H "Accept: application/xml" http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324/password/forgot/questions
View the contents of the cookie:
$ more cookies.txt # Netscape HTTP Cookie File # http://curlm.haxx.se/rfc/cookie_spec.html # This file was generated by libcurl! Edit at your own risk. localhost FALSE /openptk FALSE 0 JSESSIONID 957172ef782f03c33298e6d87e2d localhost FALSE /openptk FALSE 0 OPENPTKSESSIONID 1284708f-c9cf-4d5b-bb85-6ea25f581e61
Answer Forgot Password Questions (Phase 2)
curl -X PUT -v -b cookies.txt -H "Accept: application/json" -H "Content-Type: application/json" -d '{ "subject" : { "attributes" : { "forgottenPasswordQuestions" : ["Mothers Maiden Name","City you were born","Last 4 digits of Frequent Flyer"], "forgottenPasswordAnswers" : ["Smith","Chicago","1234"] }}}' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324/password/forgot/answers
curl -X PUT -v -b cookies.txt -H "Accept: application/xml" -H "Content-Type: application/xml" -d '<subject><attributes><forgottenPasswordQuestions type="string"><values><value>Mothers Maiden Name</value><value>City you were born</value><value>Last 4 digits of Frequent Flyer</value></values></forgottenPasswordQuestions><forgottenPasswordAnswers type="string"><values><value>Smith</value><value>Chicago</value><value>1234</value></values></forgottenPasswordAnswers></attributes></subject>' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324/password/forgot/answers
Change Password Questions (Phase 3)
Change the password with an http PUT on the password/change uri for a subject.
curl -X PUT -v -b cookies.txt -H "Accept: application/json" "Content-Type: application/json" -d '{ "subject" : { "attributes" : { "password" : "Passw0rd" }}}' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324/password/forgot/change curl -X PUT -v -b cookies.txt -H "Accept: application/json" -H "Content-Type: application/xml" -d '<subject><attributes><password type="string">Passw0rd</password></attributes></subject>' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324/password/forgot/change
Update Forgotten Password Questions:
curl -X PUT -v -H "Content-Type: application/json" -d '{"subject" : {"forgottenPasswordQuestions" : ["Mothers Maiden Name","City you were born","Last 4 digits of Frequent Flyer"], "forgottenPasswordAnswers" : ["Smith","Denver","5555"]}}' http://localhost:8080/openptk/resources/contexts/Employees-MySQL-JDBC/subjects/ja1324