REST Principles

December 13, 2012

REST stands for Representational State Transfer

5 key principles of REST are –

  • Give everything a ID

Examples
http://example.com/orders/2007/11
http://example.com/products?color=green

  • Link things together

Links help to refer to identifiable things

  • Use standard methods

GET- To retrieve a representation. Its idempotent, meaning that has no additional effect if it is called more than once with the same input parameters
PUT – update this resource with this data, or create it at this URI if it’s not there already. Its idempotent.
DELETE – delete a resource. Its idempotent.
POST – create a new resource. Its not idempotent.

  • Resources with multiple representations

Provide multiple representations of resources for different needs.

  • Communicate statelessly

A server should not have to retain some sort of communication state for any of the clients it communicates with beyond a single request. The most obvious reason for this is scalability — the number of clients interacting would seriously impact the server’s footprint if it had to keep client state.

https://www.ibm.com/developerworks/webservices/library/ws-restful/
http://tomayko.com/writings/rest-to-my-wife
http://www.infoq.com/articles/rest-introduction

Advertisements

XMLHTTP AJAX abort(), jQuery abort()

March 10, 2011

I was looking if there was a way to stop an already running AJAX request, like we can stop an http request using browser stop button. Yes there is a way to stop it using abort() method of the XMLHttpRequest object . More details about this here http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_a_1.html


jQuery JSONP (JSON with padding) – success method not called

February 23, 2011

Possible reasons for JSONP success method not getting called

  • In jQuery.getJSON method check if you are adding the
    callback=? param to your URL
  • Check if your server code serving JSONP request is returning proper JSONP response,

    For example sample server JSONP java code and response taken from IBM Developerworks article given below,
    Java code

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String jsonData = getDataAsJson(req.getParameter("symbol"));
    String output = req.getParameter("callback") + "(" + jsonData + ");";

    resp.setContentType("text/javascript");

    PrintWriter out = resp.getWriter();
    out.println(output);
    }

    JSONP Response Format

    jsonp1232617941775({"symbol" : "IBM", "price" : "91.42"});

To get a good understanding of JSONP you can read this IBM Developerworks article http://www.ibm.com/developerworks/library/wa-aj-jsonp1/


what http error code to use for validation error

February 18, 2011

I was using 500 – Internal Server Error for validation errors, but problem was the application error handler caught this and shows generic error page, which causes validation logic to fail. So instead we can use

Either use code 400 – Bad Request
or code 200 – with the error message

http://stackoverflow.com/questions/362618/proper-use-of-http-status-codes-in-a-validation-server


Handling cookies with java URLConnection

January 17, 2011

http://www.hccp.org/java-net-cookie-how-to.html

Same way you can set other headers like Content-Type, Accept to the HTTP request using the setRequestProperty method of URLConnection


Firefox addon to build HTTP request

January 17, 2011

http://stackoverflow.com/questions/725998/are-there-firefox-extension-or-any-other-browser-that-allow-to-send-arbitrary-p

This needs comes up when you cannot test build HTTP GET or POST request requiring special request headers like content type = application/json;


Apache mod_rewrite NE|noescape

August 27, 2010

I faced an issue where the URL for example /saved/ was getting rewritten to %2Fsaved%2F, which caused the web application to give 404 error because it expects the URL without escaped/encoded characters.

I found this Stack Overflow thread related to this problem which gave the solution,

http://stackoverflow.com/questions/2443115/apache-mod-rewrite-tomcat-encoding-26-and

Add NE flag to prevent escaping –

http://httpd.apache.org/docs/2.2/rewrite/rewrite_flags.html#flag_ne