REST Protocol

From the REBOL Console:

do http://reb4.me/r/rest-curl
read [
scheme: 'rest
url: http://rebol.com/
]

The REST Protocol offers an exchange-oriented implementation of HTTP. Unlike REBOL’s built-in HTTP Protocol, REST gives you greater access to both Request and Response headers in order to better access third-party APIs. Additionally, REST has OAuth signing built-in for all those hard-to-reach sites.

Requests

Making requests is easy, just use ‘read.

read [
scheme: 'rest
url: https://api.twitter.com/1.1/account/settings.json
oauth: [
Consumer-Key: "...App Key..."
Consumer-Secret: "...App Secret..."
OAuth-Token: "...User Token..."
OAuth-Token-Secret: "...User Secret..."
]
]

You can also add some parameters to your request with read/custom:

read/custom [
scheme: 'rest
url: https://example.com/upload
action: 'post
oauth: :my-oauth-settings
][
param1: "This"
param2: "That"
]

And a short-form url is available:

read rest://rebol.com
; short-form does not accommodate HTTPS

Responses

The Response object gives you immediate access to the HTTP status code and headers:

>> page: read rest://rebol.com
>> ? page
PAGE is an object of value:
status integer! 200
message string! "OK"
http-headers block! length: 14
headers object! [Date Server Last-Modified Accept-Ranges Content-E...
content string! {<!doctype html> <html><head> <meta name="generato...
binary binary! #{ 3C21646F63747970652068746D6C3E0A3C68746D6C3E3C6...
type path! length: 2
length integer! 9379

Notes

  • This version of the REST Protocol does not use REBOL’s built-in TCP Ports, rather uses the cURL shell command. cURL is available by default on most Unix/Linux/Mac OS X installations, and is generally available for most platforms. cURL is used to allow HTTPS support from the REBOL/Core interpreter.
  • This should work within most Shell environments, I have tested on CSH, BASH and SH.
  • A version of this script that uses REBOL’s internal TCP protocol is available at:
    REBOL REST; though has some caveats.
  • Please direct any questions/suggestions for now to my Twitter account: @rgrebol