A RESTful Client
Let’s learn how to develop a RESTful client.
We'll cover the following...
This lesson illustrates the development of a client for the RESTful server developed previously. However, in this case, the client acts as a testing program that tries the capabilities of the RESTful server—later in this chapter, we are going to learn how to write proper clients using cobra.
Coding example
So, the code of the client, which can be found in rClient.go, is as follows:
This same structure is found in the server implementation and is used for exchanging data.
Here, we predefine three User variables that are going to be used during testing.
The above constants define the endpoints that are going to be used.
From lines 1–5, we prepare a request that is going to access /delete using the DELETE HTTP method. On line 10, this is the correct way to specify that we want to use JSON data when interacting with the server.
Then, from lines 12–17, we send the request and wait for the server response using the Do() method with a 15-second timeout. The reason for putting that fmt.Print(), on line 27, is that we want to know the server response even if there is an error in the interaction. In ...