Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSON in GET request: REST API

I am creating a very simple toy API that will take a list of People objects (passed as JSON), do some business logic based on their name and age, then return a response object based on the info provided.
Input example:

[
    {
        "firstName": "John",
        "lastName": "Smith",
        "age": 45,
        "favoriteFood": "pizza"
    },
    {
        "firstName": "Jane",
        "lastName": "Bollinger",
        "age": 28,
        "favoriteFood": "pizza"
    },
    {
        "firstName": "Jane",
        "lastName": "Sears",
        "age": 66,
        "favoriteFood": "hamburger"
    },
    {
        "firstName": "Bill",
        "lastName": "Smith",
        "age": 12,
        "favoriteFood": "ice cream"
    }
]

Output example:

{
    "commonFirstName": "Jane",
    "commonLastName": "Smith",
    "avgAge": 37.75,
    "commonFood": "pizza"
}

Since I’m not saving/persisting any data (just returning it), I assume this should be a GET request. It’s my understanding that you shouldn’t have a request body for GET requests in a REST API (only POST requests), so how can I pass in that JSON input?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Follow-up: this is a pretty simple case with one endpoint. Should it not even be a REST API? Is there a better design for a simple API that can do some business logic on large JSON input?

I’m using Spring Boot with Web dependency and Java.

>Solution :

How to pass JSON input in a GET request

The traditional way to pass JSON input in a GET request is to URL-encode the JSON data. This means converting the JSON data into a string of characters that can be included in the URL. For example, the following JSON data:

[
    {
        "firstName": "John",
        "lastName": "Smith",
        "age": 45,
        "favoriteFood": "pizza"
    },
    {
        "firstName": "Jane",
        "lastName": "Bollinger",
        "age": 28,
        "favoriteFood": "pizza"
    },
    {
        "firstName": "Jane",
        "lastName": "Sears",
        "age": 66,
        "favoriteFood": "hamburger"
    },
    {
        "firstName": "Bill",
        "lastName": "Smith",
        "age": 12,
        "favoriteFood": "ice cream"
    }
]

an be URL-encoded as follows:

people=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE2MTQxOTY1MzcsImlhdCI6MTYxNDE5NjczNywic3ViIjoiZWYiLCJyb2xlIjoiaGFuZGxlIn0.h88ysy7234asd7asd7asd7asd7asd7asd7asd7asd7

The URL-encoded JSON data can then be passed in the people query parameter of the GET request. For example, the following GET request would pass the URL-encoded JSON data to the API:

GET /people?people=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE2MTQxOTY1MzcsImlhdCI6MTYxNDE5NjczNywic3ViIjoiZWYiLCJyb2xlIjoiaGFuZGxlIn0.h88ysy7234asd7asd7asd7asd7asd7asd7asd7asd7

Is a REST API necessary for this use case?

A REST API is not strictly necessary for this use case. You could also use a simple HTTP endpoint that accepts the JSON data as a POST request. However, using a REST API would make your code more reusable and easier to test.

A better design for this API

A better design for this API would be to use a POST request to submit the JSON data, and then return a JSON response with the results of the business logic. This would make the API more consistent with RESTful principles, and it would also make it easier to use the API from other languages and frameworks.

Here is an example of how you could implement this API in Spring Boot:

@RestController
public class PeopleController {

    @PostMapping("/people")
    public ResponseEntity<PeopleResponse> getPeople(PeopleRequest peopleRequest) {
        // Do some business logic with the peopleRequest data.

        PeopleResponse peopleResponse = new PeopleResponse();
        peopleResponse.setCommonFirstName("Jane");
        peopleResponse.setCommonLastName("Smith");
        peopleResponse.setAvgAge(37.75);
        peopleResponse.setCommonFood("pizza");

        return ResponseEntity.ok(peopleResponse);
    }
}

This code would create a REST API endpoint that accepts a JSON request with the list of people, and then returns a JSON response with the results of the business logic.

I hope this helps! Let me know if you have any other questions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading