How to use cURL with Sakai

 

This article shows you how to make requests to sakai via the command line. This is useful to making API calls to insert data into Sakai

The first step is to get a session cookie which will be stored in a file called ‘cookie.txt’. This appears to be known as a “cookie jar”!

Issue this command in a ‘terminal’ window substituting the login credentials. Do be mindful of other people seeing the credentials if working with a production system.

curl -c cookie.txt "https://trunk-maria.nightly.sakaiproject.org/api/login?username=<username>&password=<password>"

This will write a session cookie to the file cookie.txt and write out the session ID to the screen.

You can now test this out by making a (GET) request which will only work if you are authenticated. I logged in as the admin user and queried the preferences of user with ID c00d896c-57cc-4fd7-a65f-a13d08dfde:

curl -b cookie.txt 'https://trunk-maria.nightly.sakaiproject.org/direct/userPrefs/c00d896c-57cc-4fd7-a65f-a13d08dfde.json'

This returned a whole heap of JSON. Had the authentication not worked then a “403 Error Page” would have been seen.

If you are curious then try looking at the contents of cookie.txt. It should look like this:

# Netscape HTTP Cookie File

# https://curl.se/docs/http-cookies.html

# This file was generated by libcurl! Edit at your own risk.

trunk-maria.nightly.sakaiproject.org FALSE / TRUE 0 SAKAIID XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXx.ip-X-X-X-X