Nelio Alves
Nelio Alves

Reputation: 1371

How to send structured data to a REST endpoint that retrieves objects? Which HTTP verb should be used?

I want to construct a REST endpoint to retrieve objects, but I need to send structured data as query parameters (e.g. a list). I was wondering if those data could be sent as request body (see example below). How should I handle this in order to stick to REST good practices? Which HTTP verb should I use?

URI:

http://localhost:8080/products

Request Body:

{
    "name" : "Computer",
    "categories" : [
        { 
            "id" : 1

        },
        { 
            "id" : 4
        }
    ]
}

Response:

[
    {
        "id": 2,
        "name": "Computer XP 2040",
        "price": 800
    },
    {
        "id": 1,
        "name": "HP Computer",
        "price": 2000
    },
    {
        "id": 7,
        "name": "Smart Computer",
        "price": 1200
    }
]

Upvotes: 1

Views: 127

Answers (1)

Evert
Evert

Reputation: 99533

POST is not correct for this. If you want to stick to RESTful best practices, you must encode the information in the uri.

Note that POST might be better if you don't want to do this, but since this question was about REST best practices (and not http services in general), POST is your go-to.

I would simply encode this as:

 GET /products?name=Computer&categories=1,4

Upvotes: 1

Related Questions