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)
/api
- no need version control
Accept: application/json;
Accept-Language: en;
Accept-Encoding: gzip;
Vary: Accept
Vary: Accept-Language
Vary: Accept-Encoding
POST | GET | PUT | DELETE | |
---|---|---|---|---|
/products | create a new product | list of poducts | bulk update products | delete all products |
/products/:id | error | show product | if exists update, else error | delete product |
// 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);});