Git Product home page Git Product logo

restful-api-common-format-'s Introduction

rest-api-response-format

REST API response format based on some of the best practices

Rest API Popular Endpoint Formats

https://api.example.com/v1/items

https://example.com/api/v1/items

Rest API Success Responses

1- GET - Get single item - HTTP Response Code: 200

HTTP/1.1 200
Content-Type: application/json

{
    "success": true,
    "data": {
        "id": 10,
        "name": "shirt",
        "color": "red",
        "price": "$23"
        }
}

2- GET - Get item list - HTTP Response Code: 200

HTTP/1.1 200

{
    "success": true,

    "count": 100,
    "limit": 20,
    "offset": 30,

    "datas": [{
        "id": 10,
        "name": "shirt",
        "color": "red",
        "price": "$123"
        },
        {
        "id": 11,
        "name": "coat",
        "color": "black",
        "price": "$2300"
        }
    ]
}

3- POST - Create a new item - HTTP Response Code: 201

HTTP/1.1  201
Location: /v1/items/12
Content-Type: application/json

{
    "success": true,
    "data": {
        "id": 10,
        "name": "shirt",
        "color": "red",
        "price": "$23"
    }
}

4- PATCH - Update an item - HTTP Response Code: 200/204

The same response with create a new item

HTTP/1.1  200

If updated entity is not to be sent after the update

HTTP/1.1  204

5- DELETE - Delete an item - HTTP Response Code: 204

HTTP/1.1  204

Rest API Error Responses

1- GET - HTTP Response Code: 404

HTTP/1.1  404
Content-Type: application/json

{
    "success": false,
    "message": "The item does not exist"
}

2- DELETE - HTTP Response Code: 404

HTTP/1.1  404
Content-Type: application/json

{
    "success": false,
    "message": "The item does not exist"
}

3- POST - HTTP Response Code: 400

HTTP/1.1  400
Content-Type: application/json

{
    "success": false,
    "message": "Validation errors in your request", /* skip or optional error message */
    "errors": [
        {
        "message": "Oops! The value is invalid",
        "code": 34,
        "field": "email"
        },
        {
        "message": "Oops! The format is not correct",
        "code": 35,
        "field": "phoneNumber"
        }
    ]
}

4- PATCH - HTTP Response Code: 400/404

HTTP/1.1  400
Content-Type: application/json

// THE SAME WITH POST


HTTP/1.1  404
Content-Type: application/json

{
    "success" : false,
    "message": "The item does not exist"
}

5- VERB Unauthorized - HTTP Response Code: 401

HTTP/1.1  401
Content-Type: application/json

{
    "success" : false,
    "message": "Authentication credentials were missing or incorrect"
}

6- VERB Forbidden - HTTP Response Code: 403

HTTP/1.1  403
Content-Type: application/json

{
    "success" : false,
    "message": "The request is understood, but it has been refused or access is not allowed"
}

7- VERB Conflict - HTTP Response Code: 409

HTTP/1.1  409
Content-Type: application/json

{
    "success" : false,
    "message": "Any message which should help the user to resolve the conflict"
}

8- VERB Too Many Requests - HTTP Response Code: 429

HTTP/1.1  429
Content-Type: application/json

{
    "success" : false,
    "message": "The request cannot be served due to the rate limit having been exhausted for the resource"
}

9- VERB Internal Server Error - HTTP Response Code: 500

HTTP/1.1  500
Content-Type: application/json

{
    "success" : false,
    "message": "Something is broken"
}

10- VERB Service Unavailable - HTTP Response Code: 503

HTTP/1.1  503
Content-Type: application/json

{
    "success" : false,
    "message": "The server is up, but overloaded with requests. Try again later!"
}

Validation Error Formats

Validation error formats can be different depending on your requirements. Following are some other popular formats, other than the one used above.

HTTP/1.1  400
Content-Type: application/json

{
    "success": false,
    "message": "Validation errors in your request", /* skip or optional error message */
    "errors": [
        {
        "message": "Oops! The value is invalid",
        "code": 34,
        "field": "email"
        },
        {
        "message": "Oops! The format is not correct",
        "code": 35,
        "field": "phoneNumber"
        }
    ]
}

References

PATCH with partial json can be used for updating the resource: https://tools.ietf.org/html/rfc7396

Avoid using 'X-' in custom headers: https://tools.ietf.org/html/rfc6648

restful-api-common-format-'s People

Contributors

hungcv avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.