Rules To Develop An API: 1. Accept and Respond With JSON
Rules To Develop An API: 1. Accept and Respond With JSON
Content-Type: application/json
Accept: application/json
At a high-level, verbs map to CRUD operations: GET means Read, POST means Create,
PUT and PATCH mean Update, and DELETE means... well, Delete.
A response's status is specified by its status code: 1xx for information, 2xx for
success, 3xx for redirection, 4xx for client errors and 5xx for server errors.
GET: /articles/2/
POST: /articles/
400 Bad Request – This means that client-side input fails validation.
401 Unauthorized – This means the user isn’t not authorized to access a
resource. It usually returns when the user isn’t authenticated.
403 Forbidden – This means the user is authenticated, but it’s not allowed to
access a resource.
404 Not Found – This indicates that a resource is not found.
500 Internal server error – This is a generic server error. It probably
shouldn’t be thrown explicitly.
502 Bad Gateway – This indicates an invalid response from an upstream server.
503 Service Unavailable – This indicates that something unexpected happened on
server side (It can be anything like server overload, some parts of the system
failed, etc.).
Validation Errors:
{
"status": "failure",
"message": "Validation error occured"
"exceptionCode": "E_VALIDATION"
"trace": {
"surname": "This field is required."
}
}
Application Errors:
{
"status": "failure",
"message": "Data not available"
"exceptionCode": "E_DATA_NOT_AVAILABLE"
"trace": {
"error": "Expected at least two items in list."
}
}
Success response
HTTP/1.1 200 OK
Content-Type: application/json
{
"status": "success",
"message": ""
"exceptionCode": "E_NO_ERROR"
"data": {
"userId": 12123,
"userName": "Anand Kumar",
"mobileNumber": "919885109781",
"status": "A"
}
}
GET: 200 OK
POST: 201 Created
PUT: 200 OK
PATCH: 200 OK
DELETE: 204 No Content
GET: /authors/12/articles/
Option 2: use the querystring to filter the articles resource directly
GET: /articles/?author_id=12
{
"iss": "http://localhost:8000/api/auth/login", // token issuer URL
"iat": 1587957064, // issued date(unix timestamp)
"exp": 1587960664, // token expired date (unix timestamp)
"nbf": 1587957064, // the start time when token is valid from / started (unix
timestamp)
"jti": "rw0NERGU9qjhp09i", // JWT unique identifier. used mainly to prevent
redundant processing.
"sub": 15, // token title (default is user id)
"prv": "1d0a020acf5c4b6c497989df1abf0fbd4e8c8d63" // the hash value of user provider
class. especially added it in getJWTCustomClaims()
"aud": "dfhkjasdhfashdfhaskhfksah" // audience claim OPTIONAL
}