Skip to main content

HTTP Requests and Responses


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

Popular posts from this blog

Java Servlets

  According to me its just a translator, who manages the HTTP requests and handle them to the appropriate function of the API. So then, API turns out to be a term or set of those functions which are kept public. Now let us see in little bit of detail what servlets are: For that we need to see few terms: Static Web Pages: Static web pages are the pages, which are already built, and whose content and design always remain the same.  For Example: About us page of some website. Contact info page of some website Dynamic Web Pages: Dynamic pages are not already defined. They are dependent on search queries. For Example: If we search top 5 books, and click on search button, result would be different. If we search top 5 countries and enter search, result would be different.  So, the search result for every search is different. Working: Now if we have to search static page let's say the first page of any website (which is same for every use in every country) then the process is sim...

What is an API? A Simple Explanation for Programming Beginners

API What is an interface? Interface is a control system, through which we can use something. For Example: The interface of car includes: Steering Wheel Brakes Gear Acceleration pedal Mirrors Interface is concerned with the fact that how to use something, rather than how it works. The Easiest Analogy for an API : The Restaurant Imagine you're at a  restaurant . You are the user. You want food (data or a service). You can't just walk into the kitchen and start cooking. That would be  chaotic  and unsafe! Instead, you interact with the  waiter . You look at a  menu  (the  API  documentation) and give your order to the waiter. The waiter takes your request to the kitchen. The kitchen prepares your food. The waiter then brings the response (your delicious meal) back to you. The  waiter  is the contract, the messenger, the interface in our case the  API . They provide a secure and simple way for you to get what you need from the kitchen ...

Why do I need to create Java Classes? Why don't just use database for searching?

The Bridge Between Data and Display A Conceptual Guide to Java, Databases, and the "Why" behind the Code. 1. The Core Problem When building a web application, you have data sitting in a Database (SQL Server) and a user waiting at a Browser . The challenge is moving that data efficiently and safely. Beginners often ask: "Why do I need to create Java Classes ( Student.java ) and copy data into Lists? Why can't I just print the database results directly to the screen?" To answer this, we must understand how the database actually talks to Java. 2. The Anatomy of a Connection The ResultSet is NOT Data — It is a "Live Wire" A common misconception is that when you run a query, the data is instantly sent to your Java program. It is not. The ResultSet is a Cursor (Pointer) . It points to a row on the database hard drive. The Connection is a Pipe . The Streaming Analogy: Think of a ResultSet like a Phone Call . When you say rs.next() , you are asking the da...