-
Notifications
You must be signed in to change notification settings - Fork 5
Gateway Layer
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:
- http://httpbin.org/get : for GET request
- http://httpbin.org/post : for POST request
Here, url is the URL object created to establish the connection further.
We will receive the header information as below after executing above code-
We'll receive the response code as 200 - stands for 'OK', if the connection is successful, otherwise, we'll receive status code > 229.
- 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-
- 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