banner



How To Set Response Body In Rest Api

Whenever we create a Residuum API, we have to decide which parameter should exist present where.

For example, if we are creating a Remainder API to update student details using PUT (HTTP Method), then the request URI volition be{server_host}/students/{student_id} ,and the request body would be:

              {   "id": student_id,   "name": "student name",   "school_name": "school name" }            

I have seen that many times, developers get confused about why we demand to send the same parameter to multiple places. For instance, in the above example, nosotros are sending thestudent_id  to the path parameter likewise as the asking torso. Information technology may seem that we are sending repetitive data via the API, but remember that the request body and path parameters accept different meanings and should be used for the purpose that they are going to serve.Image title

Below is the explanation of why nosotros cannot removestudent_id from the path parameters and the request trunk.

Path Parameters

Path parameters are used to identify a resource uniquely. In the{server_host}/students/{student_id} example,student_id is identifying a uniquestudent_id . If we remove student_id from the path parameter, create{server_host}/studentsAPI and utilise student_id of the request torso. So, on the backend, we can write our logic perfectly fine, simply that API will not follow the REST API principle. By looking at the{server_host}/students API contract, no client would exist able to identify that this API is to process the record of but i resource.

Request Body

The request body is used to transport and receive data via the REST API. If we are using Mail service/PUT API, then based on the Rest API contract, we should send the whole resource information because these methods work on the whole resource. In the above example, student_id is as well part of that resource, then it has to exist present in the request trunk, else the asking trunk would exist able to correspond the whole resource data.

After removing student_id from the request trunk, we will have the beneath request body.

              {   "proper noun": "student proper name",   "school_name": "schoolhouse proper noun" }            

Does this request body represent the whole resource data? No. So this is a violation of the Balance contract.

And so, to stand for resource state, we demand to transport student_id in the request torso, and to place the resource uniquely, we demand to send thestudent_id in path parameter.

Thanks for reading, and let me know your thoughts in the comments.

Topics:

residue api blueprint, post methods, integration, tutorial, asking trunk, path paramenters

Opinions expressed by DZone contributors are their own.

How To Set Response Body In Rest Api,

Source: https://dzone.com/articles/rest-api-path-vs-request-body-parameters

Posted by: honigtooming76.blogspot.com

0 Response to "How To Set Response Body In Rest Api"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel