- Building RESTful Web services with Go
- Naren Yellavula
- 510字
- 2021-07-02 20:14:02
GET
A GET method fetches the given resource from the server. To specify a resource, GET uses a few types of URI queries:
- Query parameters
- Path-based parameters
In case you didn't know, all of your browsing of the web is done by performing a GET request to the server. For example, if you type www.google.com, you are actually making a GET request to fetch the search page. Here, your browser is the client and Google's web server is the backend implementer of web services. A successful GET operation returns a 200 status code.
Examples of path parameters:
Everyone knows PayPal. PayPal creates billing agreements with companies. If you register with PayPal for a payment system, they provide you with a REST API for all your billing needs. The sample GET request for getting the information of a billing agreement looks like this: /v1/payments/billing-agreements/agreement_id.
Here, the resource query is with the path parameter. When the server sees this line, it interprets it as I got an HTTP request with a need for agreement_id from the billing agreements. Then it searches through the database, goes to the billing-agreements table, and finds an agreement with the given agreement_id. If that resource exists it sends the details to copy back in response (200 OK). Or else it sends a response saying resource not found (404).
Using GET, you can also query a list of resources, instead of a single one like the preceding example. PayPal's API for getting billing transactions related to an agreement can be fetched with /v1/payments/billing-agreements/transactions. This line fetches all transactions that occurred on that billing agreement. In both, the case's data is retrieved in the form of a JSON response. The response format should be designed beforehand so that the client can consume it in the agreement.
Examples of query parameters are as follows:
- Query parameters are intended to add detailed information to identify a resource from the server. For example, take this sample fictitious API. Let us assume this API is created for fetching, creating, and updating the details of the book. A query parameter based GET request will be in this format:
/v1/books/?category=fiction&publish_date=2017
- The preceding URI has few query parameters. The URI is requesting a book from the book's resource that satisfies the following conditions:
- It should be a fiction book
- The book should have been published in the year 2017
Get all the fiction books that are released in the year 2017 is the question the client is posing to the server.
Path vs Query parameters—When to use them? It is a common rule of thumb that Query parameters are used to fetch multiple resources based on the query parameters. If a client needs a single resource with exact URI information, it can use Path parameters to specify the resource. For example, a user dashboard can be requested with Path parameters and fetch data on filtering can be modeled with Query parameters.
Use Path parameters for a single resource and Query parameters for multiple resources in a GET request.
- 智慧城市:大數據、互聯網時代的城市治理(第4版)
- Web安全防護指南:基礎篇
- MERN Quick Start Guide
- 5G承載網網絡規劃與組網設計
- 網絡創新指數研究
- 物聯網+BIM:構建數字孿生的未來
- 農產品物聯網研究與應用
- HTML5 Game development with ImpactJS
- Building RESTful Web Services with Spring 5(Second Edition)
- 6G新技術 新網絡 新通信
- OMNeT++與網絡仿真
- 網絡安全應急響應技術實戰指南
- 網絡工程實施技術與方案大全
- Qt5 Python GUI Programming Cookbook
- 移動互聯網環境下的核心網剖析及演進