Requests:
There are two types of requests:
- Idempotent
- Non-Idempotent
Idempotent:
The requests which produce same effect (do not affect the data) on repeated requests, are idempotent.
In other words, Same request can be made twice with no negative consequences to the server.
- GET
- DELETE
- PUT
These are idempotent, because once deleted or updated or read, they do not change anything on the server.
Non-Idempotent:
The request whose changes produces different effect every time, is non-idempotent. This means every time we send request to the server; the database of server is updated on each request.
- Post
- Patch
These are idempotent and needs to be managed carefully to check whether this request is previously fulfilled or not.
For Example: If we place an order using our bank account details, order would be placed. If the order is placed again at the same type (because we didn't get any response back, we place the same order again, then it would again place that order, so we need to handle this situation).
Functions:
Now there are some important request functions:
- getHeader(String);
- getIntHeader(String); //If you are sure that the content is integer, to avoid the extra step of parsing, we directly get the value of that key in integer.
Response:
Content Type:
In response, first of all we get response object and set content type, so that browser gets to now which application or assistant it has to call
- text/html (html reader)
- application/java (java renderer)
- application/JAR (JAR reader)
that's why we specify content type so that we can help the browser and save extra work and energy of identification of format.
Headers:
Now we come to the part of set Headers:
There are quite a few functions of setHeaders:
- setHeader("key", "value"); //overwrites the value if already some value is present
- addHeader("key","value"); //add more attributes
now the difference is that setHeader() allows only one value for one key:
For Example:
If we say response.setHeader("content-type", "text/html"); we all know there can only one content type, either it is a text or application or a media, so we we use addHeader("content-type", "text/html"); and then again we use addHeader("content-type", "application/jar");, then our content type attribute will have two values:
- text/html
- application/jar
and then web browser would get super confused and throw an error, so to avoid this problem, we use setHeader(); and both of them serve their own purposes.
Comments
Post a Comment