Skip to content

Gateway Layer

shubhamphepale edited this page Dec 6, 2021 · 6 revisions

Http Gateway Layer

Description

In order to get information about any http URL such as header information, response status and response body etc., by making an external API call, we need to set up a Gateway Layer in our application through which GET and POST requests can be performed.

  • The http protocol : The http protocol is the client server-based architecture where web browsers, robots and search engines, etc., acts like http clients, and the server acts as a server. The http client requests a URL, which is having a protocol version, a domain name followed by the file or directories name. The http server responds, including the message’s protocol version and a success or error code, followed by a MIME-like message containing server information, entity meta-information, and possible entity-body content.

  • HttpURLconnection : It is a http specific URLConnection. It works for http protocol only.

    • This abstract class extends URLconnection class which is present in java.net package
    • It extends from URLconnection with some extra http related functionalities.

To test our code with some useful endpoints, we needed a test server. Below are some snapshots of our code validation using the test servers as follows:

Instantiating HttpURLConnection class:

eclipse_hwjVqUtynL

Here, url is the URL object created to establish the connection further.

Reading http header information:

eclipse_U4FQwK0QPy

We will receive the header information as below after executing above code- eclipse_C9Agj0RDzS

Reading the response code of the request:

eclipse_D3uegnvXzg

We'll receive the response code as 200 - stands for 'OK', if the connection is successful, otherwise, we'll receive status code > 229.

Reading the response body of the http request:

eclipse_Z84QmbQ2cq

  • We have used StringBuilder as a mutable sequence of characters
  • InputStreamReader is there for changing byte streams to character streams
  • readLine() method from BufferedReader class helps reading the texts line by line. Refer below output from the above code- eclipse_tU5U5GriT3
  • As we can see in the above output that parameters- hero & power are displayed in the form key rather than args key. That is due to the request here being the POST request.

HTTP Gateway Video

HTTP_Gateway.mp4