SlideShare a Scribd company logo
Getting Some
      REST
with webmachine
    Kevin A. Smith
What is webmachine?
Framework
Framework
Toolkit
A toolkit for
building RESTful
     HTTP
   resources
What is
REST?
Style not a standard
Resources == URLs
http://localhost:8000/hello_world
http://foo.com/hr/emp/123
GET
 POST
DELETE
  PUT
REST is the shape
        of
    the web
-module(hello_world_resource).
-export([init/1, to_html/2]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
  {ok, "Hello, world"}.

to_html(Req, State) ->
  Output = io_lib:format(“<html><body>~s</body></html>”,
                          [State]),
  {Output, Req, State}.
-module(hello_world_resource).
-export([init/1, to_html/2]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
  {ok, "Hello, world"}.

to_html(Req, State) ->
  Output = io_lib:format(“<html><body>~s</body></html>”,
                          [State]),
  {Output, Req, State}.
-module(hello_world_resource).
-export([init/1, to_html/2]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
  {ok, "Hello, world"}.

to_html(Req, State) ->
  Output = io_lib:format(“<html><body>~s</body></html>”,
                         [State]),
  {Output, Req, State}.
-module(hello_world_resource).
-export([init/1, to_html/2]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
  {ok, "Hello, world"}.

to_html(Req, State) ->
  Output = io_lib:format(“<html><body>~s</body></html>”,
                         [State]),
  {Output, Req, State}.
-module(hello_world_resource).
-export([init/1, to_html/2]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
  {ok, "Hello, world"}.

to_html(Req, State) ->
  Output = io_lib:format(“<html><body>~s</body></html>”,
                         [State]),
  {Output, Req, State}.
A toolkit for
     easily
building RESTful
     HTTP
   resources
GET /test HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0
Accept: text/html;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: UTF-8,*
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 200 OK
Server: MochiWeb/1.1 WebMachine/1.3
Date: Mon, 22 Jun 2009 02:27:46 GMT
Content-Type: text/html
Content-Length: 78
-module(hello_world_resource).
-export([init/1, to_html/2]).
-export([generate_etag/2]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
  {ok, "Hello, world"}.

to_html(Req, State) ->
  Output = io_lib:format(“<html><body>~s</body></html>”,
                         [State]),
  {Output, Req, State}.

generate_etag(Req, State) ->
  {mochihex:to_hex(crypto:md5(State)), Req, State}.
GET /test HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0
Accept: text/html;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: UTF-8,*
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 200 OK
Server: MochiWeb/1.1 WebMachine/1.3
Etag: bc6e6f16b8a077ef5fbc8d59d0b931b9
Date: Mon, 22 Jun 2009 02:29:46 GMT
Content-Type: text/html
Content-Length: 78
GET /test HTTP/1.1
Host: localhost:8000
.
.
.
If-None-Match: bc6e6f16b8a077ef5fbc8d59d0b931b9

HTTP/1.x 304 Not Modified
Server: MochiWeb/1.1 WebMachine/1.3
Etag: bc6e6f16b8a077ef5fbc8d59d0b931b9
Date: Mon, 22 Jun 2009 02:30:00 GMT
-module(hello_world_resource).
-export([init/1, to_html/2]).
-export([generate_etag/2, encodings_provided/2]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
  {ok, "Hello, world"}.

to_html(Req, State) ->
  Output = io_lib:format(“<html><body>~s</body></html>”,
                         [State]),
  {Out, Req, State}.

generate_etag(Req, State) ->
  {mochihex:to_hex(crypto:md5(State)), Req, State}.

encodings_provided(Req, State) ->
  {[{"gzip", fun(X) -> zlib:gzip(X) end}], Req, State}.
GET /test HTTP/1.1
Host: localhost:8000
.
.
.
Accept-Encoding: gzip,deflate

HTTP/1.x 200 OK
Server: MochiWeb/1.1 WebMachine/1.3
Etag: bc6e6f16b8a077ef5fbc8d59d0b931b9
Date: Mon, 22 Jun 2009 02:46:57 GMT
Content-Type: text/html
Content-Length: 71
Content-Encoding: gzip
HTTP Is
 Hard
Getting Rest With Webmachine
Getting Rest With Webmachine
PUT
  ERROR CODES                                 vs. PO
                                                        ST
                              O N
                          I
                     TI AT                   ENC
                    O                               ODI
                 NEG                                    N      GS
               T
        T EN           IDEMPOTENCY                           N
    N                                                      IO
 C O
                                                     IR AT
                                                  X P
                     STREA                      E
                          MING                T
OVE                                     T   EN                         ET
    RLOA                             O N                              G
               DED                  C                          A    L
                     POS                                    O N
                        Ts                              I TI
                                                 ND
                                              CO
Request
Routing
http://foo.com/items

{["items"], grocery_item_resource, []}.


 URL path      Resource module     Init params
http://foo.com/items/chocolate

{["items", item], grocery_item_resource, []}.




URL path   URL variable   Resource module   Init params
GET
-module(hello_world_resource).
-export([init/1,to_html/2]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
  {ok, "Hello, world"}.

to_html(Req, State) ->
  Output = io_lib:format(“<html><body>~s</body></html>”,
                          [State]),
  {Output, Req, State}.
Must be idempotent
PUT
-module(grocery_list_resource).
-export([init/1,allowed_methods/2]).
-export([content_types_accepted/2,from_json/2]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
  {ok, []}.

allowed_methods(Req, State) ->
  {[‘PUT’], Req, State}.

content_types_accepted(Req, State) ->
  {[{“application/json”, from_json}], Req, State}.

from_json(Req, State) ->
  %% Create/update logic here
DELETE
-module(grocery_list_resource).
-export([init/1,allowed_methods/2]).
-export([delete_resource/2]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
  {ok, []}.

allowed_methods(Req, State) ->
  {[‘DELETE’], Req, State}.

delete_resource(Req, State) ->
  %% Deletion logic here
POST
Hmmmm
Problems with
    POST
Problems with
           POST

• Overloaded semantics
Problems with
           POST

• Overloaded semantics
• Create or update?
Creation via
        POST
•allowed_methods/2
•post_is_create/2
•create_path/2
•content_types_accepted/2
• handler function
Update via
         POST

•allowed_methods/2
•content_types_accepted/2
• handler function
CODE
TIME
HTTP Request
  Access
HTTP Request

• “Wrapped” mochiweb request
• Separate process for each request
• Direct access to: request/response
  headers, response body, etc.
Other Cool
            Stuff

• Graphical request debugging
• Streaming requests and responses
• File uploads
webmachine source:
       http://bitbucket.org/justin/webmachine


               Slides and demo code:
http://github.com/kevsmith/erlang_factory/tree/master

More Related Content

KEY
groovy & grails - lecture 3
ODP
Php variables (english)
PPT
Introduction to PHP
KEY
Unfiltered Unveiled
PDF
PHP for Grown-ups
PDF
Beginning web programming with PHP [PHP 101-02]
PDF
Data Types In PHP
PDF
2014 database - course 2 - php
groovy & grails - lecture 3
Php variables (english)
Introduction to PHP
Unfiltered Unveiled
PHP for Grown-ups
Beginning web programming with PHP [PHP 101-02]
Data Types In PHP
2014 database - course 2 - php

What's hot (14)

PPT
Php Lecture Notes
PPT
PHP variables
PPT
PHP POWERPOINT SLIDES
PPT
Php mysql
PPT
Class 2 - Introduction to PHP
PPT
My cool new Slideshow!
PPT
slidesharenew1
PDF
Opa hackathon
KEY
Intermediate PHP
PDF
Web 9 | OOP in PHP
PDF
RCEC Email 4.7.03 (b)
PDF
Introduction to PHP - Basics of PHP
PPT
Intro to php
ODP
OpenGurukul : Language : PHP
Php Lecture Notes
PHP variables
PHP POWERPOINT SLIDES
Php mysql
Class 2 - Introduction to PHP
My cool new Slideshow!
slidesharenew1
Opa hackathon
Intermediate PHP
Web 9 | OOP in PHP
RCEC Email 4.7.03 (b)
Introduction to PHP - Basics of PHP
Intro to php
OpenGurukul : Language : PHP
Ad

Viewers also liked (9)

PPTX
TravisCIでErlang/OTP (最小構成版)
PDF
中高生向けUnity講座
PPTX
Unityで使うRabbitMQ
ODP
非同期データ更新のためにメッセージキューを導入した(い)話
PPTX
Elixirについて私が知ってる二、三の事柄
PPTX
Shibuya.ex #1 Elixirを本番環境で使ってみたという事例紹介
PDF
パーフェクト"Elixir情報収集"
PPTX
地獄のElixir(目黒スタートアップ勉強会)
PPTX
Erlangご紹介 websocket編
TravisCIでErlang/OTP (最小構成版)
中高生向けUnity講座
Unityで使うRabbitMQ
非同期データ更新のためにメッセージキューを導入した(い)話
Elixirについて私が知ってる二、三の事柄
Shibuya.ex #1 Elixirを本番環境で使ってみたという事例紹介
パーフェクト"Elixir情報収集"
地獄のElixir(目黒スタートアップ勉強会)
Erlangご紹介 websocket編
Ad

Similar to Getting Rest With Webmachine (20)

PDF
Tech night ldn - chatbot
PDF
Building Tools with GitHub Customize Your Workflow 1st Edition Chris Dawson
PDF
PDF
Restful Web Services Cookbook 1st Edition Subbu Allamaraju
PDF
So you think you know REST - DPC11
PPTX
ECSA'15 - Modeling REST-ful conversations
PDF
From zero to almost rails in about a million slides...
PDF
Build web server
PPT
Faster! Faster! Accelerate your business with blazing prototypes
PDF
Learn REST in 18 Slides
KEY
Something something rack
PDF
Java Network Programming Fourth Edition Elliotte Rusty Harold
KEY
Api anti patterns
PDF
distributing over the web
PPTX
REST library.pptx
PDF
Java Network Programming Fourth Edition Elliotte Rusty Harold
PDF
Building Hypermedia Apis With Html5 And Node Mike Amundsen
PDF
Designing Evolvable Web Apis With Aspnet 1st Edition Glenn Block
PDF
Rest Vs Soap Yawn2289
PDF
Node Up and Running Scalable Server Side Code with JavaScript 1st Edition Tom...
Tech night ldn - chatbot
Building Tools with GitHub Customize Your Workflow 1st Edition Chris Dawson
Restful Web Services Cookbook 1st Edition Subbu Allamaraju
So you think you know REST - DPC11
ECSA'15 - Modeling REST-ful conversations
From zero to almost rails in about a million slides...
Build web server
Faster! Faster! Accelerate your business with blazing prototypes
Learn REST in 18 Slides
Something something rack
Java Network Programming Fourth Edition Elliotte Rusty Harold
Api anti patterns
distributing over the web
REST library.pptx
Java Network Programming Fourth Edition Elliotte Rusty Harold
Building Hypermedia Apis With Html5 And Node Mike Amundsen
Designing Evolvable Web Apis With Aspnet 1st Edition Glenn Block
Rest Vs Soap Yawn2289
Node Up and Running Scalable Server Side Code with JavaScript 1st Edition Tom...

Recently uploaded (20)

PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
1. Introduction to Computer Programming.pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Encapsulation theory and applications.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPT
Teaching material agriculture food technology
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Approach and Philosophy of On baking technology
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Building Integrated photovoltaic BIPV_UPV.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Machine learning based COVID-19 study performance prediction
SOPHOS-XG Firewall Administrator PPT.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
OMC Textile Division Presentation 2021.pptx
NewMind AI Weekly Chronicles - August'25-Week II
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
1. Introduction to Computer Programming.pptx
A comparative study of natural language inference in Swahili using monolingua...
A comparative analysis of optical character recognition models for extracting...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Encapsulation theory and applications.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Teaching material agriculture food technology
Group 1 Presentation -Planning and Decision Making .pptx
Approach and Philosophy of On baking technology

Getting Rest With Webmachine