Thursday, 18 July 2019

REST API interview Questions


In this post, I'm going to share REST API interview questions actually asked in I.T. companies.

Question 1:

What exactly is RESTful programming?

Answer:
 
An architectural style called  REST [Representational State Transfer] states that Web applications should use HTTP methods for communication.
GET should be used for lookups.
PUT, POST and DELETE requests should be used for mutation, creation and deletion respectively.

Keep in mind that GET request should never be used for updating information. e.g. a GET request for adding an item to a cart :

http://javafreeway/addToCart?cartId=12345&item=4321    would not be appropriate.

GET requests should be idempotent. That means, issuing a request twice should be no different from issuing it once. That's what makes the request cacheable.
An addToCart request is not idempotent, because issuing it twice will add two copies of same item to the cart. A POST request is clearly appropriate in this context.

Programmatic Explanation:

Create a user with three properties:

POST /user
fname=Johnny&lname=Joe&age=30

The server responds:
200 OK
location: /user/321

Now, we can retrieve the user information:

GET /user/321

The server responds:

200 OK
<fname>johnny</fname><lname>Joe</lname><age>30</age>

To modify the record (age and lname will remain unchanged)

PATCH /user/321
fname=john


To update the record (lname and age will become NULL)

PUT /user/321
fname=john


Question 2:

What is the difference between REST Endpoints and REST resources?

Answer:

Resource is a RESTful subset of endpoint.

REST endpoints are used to define REST requests.
REST resources are used to define Data sets which are returned.

In simple words,  endpoints represents URL.
And resource represents the data to be returned.

e.g:

An endpoint is the location where a service can be accessed.

http://javafreeway.blogspot.com
services/service.asmx

A resource refers to one or more nouns being served.

/api/users/johnny              // look up johnny from a list of users.
/v2/books/4321                //  Get book with ID 4321 in API v2 schema

All of the above are service endpoints, but the bottom ones are for resources.


Question 3:

What is the most popular way to present a resource in REST?

Answer:

XML, JSON.

Question 4:

What are the core components of an HTTP request?

Answer:

Each HTTP request includes 5 key elements:

  • The verbs : GET, POST, PUT, DELETE, PATCH
  • URI: Uniform Resource Identifier. [Identifies resource on the server]
  • HTTP version
  • Request header: Carries metadata for the HTTP request message
  • Request Body: represents message content

Question 5:

What are the core components of an HTTP response?

Answer:

An HTTP response includes 4 key elements:

  • Status/Response code: indicates server status for the resource present in HTTP request. e.g. 404 means "resource not found"  and 200 means , "response is OK".
  • HTTP Version
  • Response Header: contains metadata for HTTP response
  • Response Body: Indicates response message content

Question 6:

What is the difference between PUT and POST operations?

Answer:

                    PUT                                                                                        POST

PUT is used for update operation                                    POST is used for create operation

PUT method is idempotent                                              POST is not idempotent

Use PUT when you want to modify a singular                Use POST when you want to add a
resource which is already a part of resources                   child resource under resources
collection. PUT replaces the resource in its                      collection
entirety. Use PATCH if request updates part
of the resource.                            
                                                                                          
PUT is idempotent, so you can cache the                         Responses to this method are not
response                                                                             cacheable, unless the response
                                                                                          includes appropriate  Cache-Control
                                                                                          fields.

Generally, always use PUT for update                             Always use POST for create
operations.                                                                         operations.


Question 7:

What are the tools available for testing Web services?

Answer:
  • SOAP UI tool
  • Poster for Firefox Browser
  • The Postman extension for Chrome.

Question 8:

Which Java API helps in developing a RESTful Web Service?

Answer:

There are many frameworks and libraries available that a developer can use to create RESTful web services in java.
e.g.: the JAX-RS library is a standard way to develop a REST web service.

Also Jersey is another most popular implementations of JAX-RS.
There are others like RESTEasy, RESTlet and Apache CFX.

 If you liked this post, then Hit 'LIKE' button.


No comments:

Post a Comment