0

I'm interested in knowing various ways I can fire HTTP requests involving different Methods

Ex. HTTP request involving GET method can be fired in following ways

  1. type URL in browser and hit enter
  2. click a hyper link
  3. submit a HTML form without specifying method attribute
  4. submit a HTML form with method attribute "GET"

Only way of firing HTTP request involving POST method is to submit a HTML form with method attribute "POST"

I've already experimented on GET & POST using Java Servlets and am curious to test HEAD, PUT, DELETE, TRACE, OPTIONS using Java Servlets

Waiting for suggestions from Java/HTML/HTTP Gurus

FYI, I'm using Apache Tomcat 7.0.47 for experimenting all this stuff

Thanks in advance

Kevin Panko
  • 7,844
  • 19
  • 46
  • 58
JavaHopper
  • 5,537
  • 1
  • 18
  • 27
  • Browsers usually don't send requests other than POST and GET, but some Javascirpt libraries can. If you are curious about REST, you shall consider clients other than browsers, consider that REST is a paradigm shift from HTTP based RPCs. – Amir Pashazadeh Dec 09 '13 at 17:26
  • Hi Amir, is there no other way to send request other than GET/POST from a browser using simple HTML? Thanks for reminding, we can use AJAX to send various methods.. But I'm just a beginner and want to simulate this using HTML, to start with – JavaHopper Dec 09 '13 at 17:30
  • @JavaHopper No, [HTML currently does not support any requests other than GET and POST.](http://programmers.stackexchange.com/questions/114156/why-there-are-no-put-and-delete-methods-in-html-forms) – Sotirios Delimanolis Dec 09 '13 at 18:29
  • By the way, if you want to implement a RESTful webservice, don't reinvent the wheel! Use JAX-RS or similart things. – Amir Pashazadeh Dec 09 '13 at 21:59
  • Thanks Sotirios for the link and Amir for your webservice example. – JavaHopper Dec 10 '13 at 04:20

1 Answers1

0

HTTP is a standard protocol. It has a standard request and response format. It is sent over TCP. You have HTTP servers listening on some port to respond to requests and HTTP clients sending those requests.

At the most basic level, an HTTP client can use a Socket and a SocketOutputStream to send the HTTP request. Take a look at this example. You simply write bytes (from the serialized request String) to the socket output stream. The HTTP server parses the bytes and if it is in the right format, dispatches some handler to handle the request.

There are a lot of Java libraries out there that simplify the task for you. For example, Java's HttpUrlConnection or Apache's HTTP Components.

On the client side, sending a HEAD, PUT, DELETE, TRACE, OPTIONS request is no different than a GET or POST, except in what the request line and body of the request might contain.

On the server side, implemented with Servlets, you simply make your class extend HttpServlet and implement the various doXxx() methods.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683
  • I tried submitting below form to send HEAD
    ....
    I still see in Fiddler(HTTP Proxy/Debugging Tool) a GET being fired, rather than HEAD Any specific reason? Theoretically, I know that HEAD is only to get the response header info.. But it displays the output of service(ServletRequest, ServletResponse) method present in my servlet
    – JavaHopper Dec 09 '13 at 17:27
  • @JavaHopper The entry point of all `Servlet` classes is the `service` method. If your class doesn't extend `HttpServlet` you will have to implement your own dispatching routines for handling the different http methods. – Sotirios Delimanolis Dec 09 '13 at 18:07
  • Sotirios, Digging a bit further, I wrote a class extending HttpServlet and I've overridden doHead(HttpServletRequest, HttpServletResponse) method.. And still I was getting "HTTP Status 405 - HTTP method GET is not supported by this URL" Going by your earlier link, and looking at the fiddler trace, browser (IE-10) is sending GET request for methods other than POST.. May be my research can continue on this at time when I start exploring advanced technologies such as AJAX/Web Services – JavaHopper Dec 10 '13 at 04:20
  • @JavaHopper As stated in the comments to your question, HTML doesn't support other methods, only GET and POST. You can use a different http client to test your servlet. – Sotirios Delimanolis Dec 10 '13 at 04:29