iegik
1/3/2015 - 7:25 PM

cURL

RESTful cURL

## cURL

# GET \
curl -i -X GET http://localhost:5000/api/products \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "Accept-Language: en" \
  -H "Accept-Encoding: gzip" \

# GET
curl -i -X GET http://localhost:5000/api/products/54a8391e64cc0b994d57ba63 \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "Accept-Language: en" \
  -H "Accept-Encoding: gzip" \

#PUT
curl -i -X PUT http://localhost:5000/api/products/54a8391e64cc0b994d57ba63 \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "Accept-Language: en" \
  -H "Accept-Encoding: gzip" \
  -d  "{\
  \"price\": 21\
}" \

# POST
curl -i -X POST http://localhost:5000/api/products \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "Accept-Language: en" \
  -H "Accept-Encoding: gzip" \
  -d "{\
  \"name\":\"My product\",\
  \"sku\":\"ASDF1234\",\
  \"price\": 32.50\
}" \

# DELETE
curl -i -X DELETE http://localhost:5000/api/products/54a8391e64cc0b994d57ba63 \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "Accept-Language: en" \
  -H "Accept-Encoding: gzip" \

### Authorisation
#### NTLM

curl --negotiate -u : -b ~/cookie.tmp -c ~/cookie.tmp -ik -X POST https://example.com/Authentication?login=true
curl --ntlm -u {DOMAIN}\\{login}:{pass} -b ~/cookie.tmp -c ~/cookie.tmp -ik -X POST https://example.com/service \
  -H "Content-Type: text/xml" \
  -H "Accept: text/xml" \
  -H "Accept-Language: en" \
# ...


# curl
# -c, --cookie-jar FILE  Write cookies to FILE after operation (H)
# --negotiate         Use HTTP Negotiate (SPNEGO) authentication (H)
# --ntlm              Use HTTP NTLM authentication (H)
# -d, --data DATA     HTTP POST data (H)
# -H, --header LINE   Pass custom header LINE to server (H)
# -k, --insecure      Allow connections to SSL sites without certs (H)
# -i, --include       Include protocol headers in the output (H/F)

RESTful API

/api - no need version control

Request headers:

Accept: application/json;
Accept-Language: en;
Accept-Encoding: gzip;

Response headers:

Vary: Accept
Vary: Accept-Language
Vary: Accept-Encoding

Products

POSTGETPUTDELETE
/productscreate a new productlist of poductsbulk update productsdelete all products
/products/:iderrorshow productif exists update, else errordelete product

Idempotency of RESTFul methods:

  • POST – Creates a new resource. POST is not idempotent and it is not safe.
  • GET – Retrieves a resource. GET is idempotent and it is safe.
  • HEAD – Retrieves a resource (without response body). HEAD is idempotent and it is safe
  • PUT – Updates/replaces a resource. PUT is idempotent but it is not safe
  • PATCH – Partially updates a resource. PATCH is not idempotent and it is not safe.
  • DELETE – Deletes a resource. DELETE is idempotent but it is not safe.
  • TRACE – Performs a loop-back test. TRACE is idempotent but it is not safe.
// JavaScript

// Helpers

function serialize(o){
    return Object.keys(o).reduce(function (previous, key) {
        return previous + (previous?'&':'') + key + '=' + o[key];
    },'');
}

// AJAX
function ajax(method, href, data, headers) {
    var url = new URL((/^\//.test(href)?document.location.origin:'')+href);
    var ajax = new XMLHttpRequest();
    ajax.open('POST',url.origin + url.pathname,1);
    ajax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    ajax.send(url.search.substr(1));
    return ajax;
}
// (function(a,b){function send(i){return ajax('POST','/?n='+(i+a))}Array(b).join('.').split('.').map(function(x,i){return send(i));})();

// Simulating FORM
function form(method, href, data, headers) {
    var url = new URL((/^\//.test(href)?document.location.origin:'')+href);
    return $('<form method="' + method + '" action="' + url.origin + url.pathname + '">' + Object.keys(data).reduce(function (previous, key) {
        return previous + '<input name="' + key + '" value="' + data[key] + '"/>';
    },'') + '</form>').submit();
};
// (function(a,b){function send(i){return form('POST','/?n='+(i+a))}Array(b).join('.').split('.').map(function(x,i){return send(i));})();

// Angular
angular.element(document).injector().get('$http').get('/api/products').then(function(res){console.dir(res.data);});