REST API URI design with Order management example.

REST is a software architectural style that helps to design web services. It helps systems to communicate with interoperability.

The operations in REST available are GET, HEAD, POST, PUT, PATCH, DELETE, CONNECT, OPTIONS and TRACE.

Lets see API design with Order management example for commonly used methods.

POST: Create an Order with the provided input data in the payload.

const http = require(‘http’)const data = JSON.stringify({
order: ‘order info’
})
const options = {
hostname: ‘localhost’,
port: 8080,
path: ‘/api/v1/orders’,
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’,
‘Content-Length’: data.length
}
}
const req = http.request(options, res => {
res.on(‘data’, data => {
//logic goes here
})
})
req.on(‘error’, error => {
console.error(error)
})
req.write(data)
req.end()

PUT: Update the Order with the help of Order data.

PUT is similar to POST, just need to replace POST in the above example.

DELETE: Delete the Order details with the help of Order Id.

DELETE is similar to POST, just need to replace POST in the above example.

GET: Get the Order with the help of orderId.

const http = require(‘http’)
const options = {
hostname: ‘localhost’,
port: 8080,
path: ‘/api/v1/orders/12345’,
method: ‘GET’
}
const req = http.request(options, res => {
res.on(‘data’, d => {
//logic goes here
})
})
req.on(‘error’, error => {
console.error(error)
})
req.end()

GET: Get all the Orders

const http = require(‘http’)
const options = {
hostname: ‘localhost’,
port: 8080,
path: ‘/api/v1/orders’,
method: ‘GET’
}
const req = http.request(options, res => {
res.on(‘data’, d => {
//logic goes here
})
})
req.on(‘error’, error => {
console.error(error)
})
req.end()

To read about best practices to implement REST API visit the below link.

https://muralee1857.medium.com/best-practices-to-design-rest-api-calls-a53381c4aa68