timo
timo

Reputation: 113

why does the program need to check API header "Accept"

I am using swagger codegen to generate the controller for me. And there is a if statement there but it seems to me it is not necessary. Thank you.

        String accept = request.getHeader("Accept");
        if (accept != null && accept.contains("application/json"))

Upvotes: 0

Views: 560

Answers (2)

Gaurav Jeswani
Gaurav Jeswani

Reputation: 4592

Your swagger document must have something like below to API method:

responses:
   "200":
     description: OK
     content:
       application/json:

As per this, the response of your API is of type application/json:. But additionally in future, if the server decides to produce some other type of response also as below:

 responses:
   "200":
     content:
       image/svg+xml:
         schema:
           type: string
       application/json:
         schema:
           $ref: "#/components/schemas/MyDto"

In this case to decide the response type in response Accept parameter would be necessary. So in my opinion there are 2 reasons for this condition to generate:

  1. That Client and Server are in same contract for returning content.

  2. If tomorrow new content type is added old code is not breaking.

Upvotes: 2

chytonpide
chytonpide

Reputation: 121

Content-Type: it is a header that tells the format of data sent in HTTP messages (both requests and responses).

Accept: it is a header that is placed in a request when requested from a browser (client) to a web server. This Accept header simply means that I(client) will only allow you this data type as a response.

If the server supports multiple data types, The server determines a data type of response using a accept header like this,

@GetMapping("/someresources")
public ResponseEntity<String> getSomeresources(@RequestHeader("accept") String accept) {
        
        List<SomeResource> someresources = someService.someresources();
        
        //for react app
        if(accept.contains("application/json")) {
            SomeresourcesRepresentation representation = new SomeresourcesRepresentation(someresources);
            String serialziedRepresentaiton = jsonSerializer.serialize(representation);
            
            ResponseEntity<String> response = ResponseEntity
                    .status(HttpStatus.OK)
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(serialziedRepresentaiton);
            
            return response;
        } 

        //for web browser
        if(accept.contains("text/html")) {
            
            String html = "<!doctype html>"
                    + "<html>"
                    + "<head>"
                    + "<meta charset='UTF-8'>"
                    + "<title>summary</title>"
                    + "</head>"
                    + "<body>"
                    + "someresources size : "+someresources.size()
                    + "</body>"
                    + "</html>";
            
            ResponseEntity<String> response = ResponseEntity
                    .status(HttpStatus.OK)
                    .contentType(MediaType.TEXT_HTML)
                    .body(html);

            return response;
        }
        
        return ResponseEntity.notFound().build();
    }

Upvotes: 2

Related Questions