JSON Complex Request Payload - Notes By ShariqSP
JSON Complex Request Payload
Base URL:
http://apitesting.shariqsp.com:8080
Endpoints
1. GET /api/companies
This endpoint retrieves all the companies and their details, including mobile items, specifications, warranties, and availability in different stores.
Request Example:
GET http://apitesting.shariqsp.com:8080/api/companies
Response Example:
[
{
"id": 5,
"name": "TechCorp",
"location": "New York",
"phone": "+1234567890",
"email": "contact@techcorp.com",
"lastUpdated": "2024-11-25T07:17:36.204Z",
"mobileItems": [
{
"id": 5,
"brand": "Samsung",
"model": "Galaxy S23 Ultra",
"price": 1199.99,
"currency": "USD",
"specifications": {
"processor": "Snapdragon 8 Gen 2",
"ram": "12GB",
"storage": "256GB",
"rearCamera": "200MP",
"frontCamera": "40MP",
"battery": 5000,
"os": "Android 13"
},
"warranty": {
"coverage": "Manufacturer",
"duration": 2
},
"availability": [
{
"store": "Best Buy",
"stock": 25
},
{
"store": "Amazon",
"stock": 50
}
]
}
]
}
]
Response Fields:
- id: Company ID
- name: Company Name
- location: Location of the company
- phone: Contact phone number
- email: Company email
- lastUpdated: Last updated timestamp
- mobileItems: Array of mobile items related to the company
2. POST /api/companies
This endpoint is used to create a new company along with its mobile items.
Request Example:
POST http://apitesting.shariqsp.com:8080/api/companies
Request Body Example:
{
"name": "TechCorp",
"location": "New York",
"phone": "+1234567890",
"email": "contact@techcorp.com",
"lastUpdated": "2024-11-25T07:17:36.204Z",
"mobileItems": [
{
"brand": "Samsung",
"model": "Galaxy S23 Ultra",
"price": 1199.99,
"currency": "USD",
"specifications": {
"processor": "Snapdragon 8 Gen 2",
"ram": "12GB",
"storage": "256GB",
"rearCamera": "200MP",
"frontCamera": "40MP",
"battery": 5000,
"os": "Android 13"
},
"warranty": {
"coverage": "Manufacturer",
"duration": 2
},
"availability": [
{
"store": "Best Buy",
"stock": 25
},
{
"store": "Amazon",
"stock": 50
}
]
}
]
}
Response Example:
{
"id": 6,
"name": "TechCorp",
"location": "New York",
"phone": "+1234567890",
"email": "contact@techcorp.com",
"lastUpdated": "2024-11-25T07:17:36.204Z",
"mobileItems": [
{
"id": 6,
"brand": "Samsung",
"model": "Galaxy S23 Ultra",
"price": 1199.99,
"currency": "USD",
"specifications": {
"processor": "Snapdragon 8 Gen 2",
"ram": "12GB",
"storage": "256GB",
"rearCamera": "200MP",
"frontCamera": "40MP",
"battery": 5000,
"os": "Android 13"
},
"warranty": {
"coverage": "Manufacturer",
"duration": 2
},
"availability": [
{
"store": "Best Buy",
"stock": 25
},
{
"store": "Amazon",
"stock": 50
}
]
}
]
}
Request Body Fields:
- name: Company name (String)
- location: Location of the company (String)
- phone: Contact phone number (String)
- email: Contact email (String)
- lastUpdated: Timestamp when the company data was last updated (ISO 8601 format)
- mobileItems: Array of mobile items associated with the company
Error Responses
- 400 Bad Request: The server returns this status when the request body is malformed or missing required fields.
- 404 Not Found: The endpoint could not be found. Ensure the URL is correct.
- 500 Internal Server Error: An unexpected error occurred on the server while processing the request.
How to Test API with Swagger UI
- Open Swagger UI:
Visit the Swagger UI page at http://apitesting.shariqsp.com:8080/swagger-ui/index.html. - Interact with the Endpoints:
- ForGET /api/companies:
Click on theGETmethod next to/api/companies, click "Try it out", then "Execute" to see the response. - ForPOST /api/companies:
Click on thePOSTmethod next to/api/companies, click "Try it out", provide the request body in JSON format, then click "Execute" to submit the data.
Conclusion
- GET /api/companies: Fetches all companies and their associated mobile items.
- POST /api/companies: Adds a new company along with mobile items and their availability.
You can test these endpoints easily with the Swagger UI interface, making the interaction with the API more intuitive. Ensure that the request payloads are correctly formatted to avoid errors when sending data to the server.