0% found this document useful (0 votes)
18 views

Devops

Uploaded by

Ragul Ragul
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Devops

Uploaded by

Ragul Ragul
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Stopwatch Application Documentation

Project Overview:

The HTTP Client Application is a standalone Java-based program that provides basic
functionality for sending HTTP GET and POST requests to web APIs. It allows users to
test API endpoints and view responses in a user-friendly console interface.

Key Features:

 GET Request: Allows users to send a GET request to a specified URL and receive a response. This
can be useful for retrieving information from APIs or testing API endpoints.

 POST Request: Enables users to send data in JSON format to a specified URL using a POST
request. The response from the server is displayed to confirm successful data submission.

 Response Display: Displays the API responses in a structured format, showing response data and
status

Project Setup:

1. Java JDK: Ensure Java Development Kit 8 or higher is installed.


2. Eclipse IDE: The project was developed in Eclipse, but it can be run in any Java-
supported IDE. Additionally, make sure that Apache HTTP Client dependencies
are included in the pom.xml if using Maven..

Step-by-Step Guide:

1. Create a New Maven Project:


a. Group ID:
b. Artifact ID:
c. Ensure it’s set up as a standalone project (no parent project).
2. Set Up Project Structure:
a. Verify src/main/java for application code and pom.xml for dependencies.

Maven Configuration (pom.xml):


Below is the pom.xml file with dependencies and plugins configured for this project
3. Implement the HTTP Client Class:

 Package: com.example.httpclient

 Class: HttpClientExample

 Functions Implemented:

 makeGetRequest(): Sends an HTTP GET request.


 makePostRequest(): Sends an HTTP POST request with a JSON payload.
 printResponse(): Displays the HTTP response status code and body.
a. Testing the HTTP Client
The main method demonstrates how to call the makeGetRequest and
makePostRequest functions. It prints the response code and body for each
request.

Sample Code:
Package com.example.httpclient;

Import org.apache.http.HttpEntity;
Import org.apache.http.client.methods.CloseableHttpResponse;
Import org.apache.http.client.methods.HttpGet;
Import org.apache.http.client.methods.HttpPost;
Import org.apache.http.entity.StringEntity;
Import org.apache.http.impl.client.CloseableHttpClient;
Import org.apache.http.impl.client.HttpClients;
Import org.apache.http.util.EntityUtils;

Public class HttpClientExample {

// Method for GET request


Public void sendGetRequest(String url) throws Exception {
Try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpGet request = new HttpGet(url);
Try (CloseableHttpResponse response = client.execute(request)) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
System.out.println(“GET Response: “ + result);
}
}
}

// Method for POST request


Public void sendPostRequest(String url, String json) throws Exception {
Try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost request = new HttpPost(url);
Request.setEntity(new StringEntity(json));
Request.setHeader(“Content-type”, “application/json”);

Try (CloseableHttpResponse response = client.execute(request)) {


HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
System.out.println(“POST Response: “ + result);
}
}
}

Public static void main(String[] args) {


HttpClientExample httpClientExample = new HttpClientExample();
Try {
// Test GET Request
httpClientExample.sendGetRequest(https://jsonplaceholder.typicode.com/
posts/1);

// Test POST Request


String json = “{\”title\”: \”foo\”, \”body\”: \”bar\”, \”userId\”: 1}”;
httpClientExample.sendPostRequest(https://jsonplaceholder.typicode.com/posts,
json);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output Example:
GET Request Example
 For a GET request to the URL https://jsonplaceholder.typicode.com/posts/1, the
console output would look like this:GET Request:
 Response Code: 200
{
“userId”: 1,
“id”: 1,
“title”: “sunt aut facere repellat provident occaecati excepturi optio
reprehenderit”,
“body”: “quia et suscipit\nsuscipit …
}

OUTPUT

You might also like