(source: unsplash.com)
This lesson requires the knowledge of JSON encoding/decoding, data streaming, file system, and other essential concepts. I advise you to get read these lessons before proceeding with this tutorial.
When a browser sends an HTTP request to a server, it sends a request object that contains data to be sent to the server and some meta-data that describes the nature of the request as well as some session information. When the server receives the request, it can respond with some data and meta-data.
A backend (server) can also act as a browser and it too can send HTTP requests to fetch some data from a remote server. However, the mechanism to send a request and handle the response has to be completely manual.
When we send an HTTP request to a remote server in Go and we get a response object in the shape of as a *http.``[Response](https://golang.org/pkg/net/http/#Response)
structure.
type Response struct {
Status string // e.g. "200 OK"
StatusCode int // e.g. 200
Proto string // e.g. "HTTP/1.0"
ProtoMajor int // e.g. 1
ProtoMinor int // e.g. 0
// response headers
Header http.Header
// response body
Body io.ReadCloser
// request that was sent to obtain the response
Request *http.Request
}
To make an HTTP request, we need a *http.``[Client](https://golang.org/pkg/net/http/#Client)
struct object. However, Go provides a global *Client
struct through http.``[DefaultClient](https://golang.org/pkg/net/http/#DefaultClient)
variable. This is a minimal configuration of the Client
structure and enough to send HTTP requests. We will learn how to customize the Client
structure later.
var DefaultClient = &http.Client{}
The *http.Client
provides some methods to send some simple HTTP requests. The http
package exposes some functions that call these methods on http.DefaultClient
object internally. These are as follows.
http.Get
functionThe http.``[Get](https://golang.org/pkg/net/http/#Get)
function sends a GET
request to a remote server. This function internally calls the [Get](https://golang.org/pkg/net/http/#Client.Get)
method on the http.DefaultClient
client.
func Get(url string) (res *Response, err error)
The url
is a fully qualified [URL](https://en.wikipedia.org/wiki/URL)
string. If the server responds with a redirect, http.Get
follows the redirect URL up to a maximum of 10
redirects. A non-2xx
response does not result in an error but we can use res.StatusCode
to check if the response code is 4xx
or 5xx
.
This method returns a non-nil
error if the server responds with more than 10
redirects or when the request timeouts or where there is an HTTP protocol error. Any error returned by this method will be of *url.``[Error](https://golang.org/pkg/net/url/#Error)
type.
If we look at the field of http.``[Response](https://golang.org/pkg/net/http/#Response)
object, the most important fields are StatusCode
of int
type that indicates the HTTP response status code, the Header
field of type http.``[Header](https://golang.org/pkg/net/http/#Header)
(derived from the _map_
type) which contains the response headers and Body
of io.``[ReadCloser](https://golang.org/pkg/io/#ReadCloser)
interface type that contains the raw response body.
When the error is nil
, the response a body res.Body
always contain non-nil
response. Let’s make a sample request to fetch some JSON from a mock data website. You can visit this website to look at some public mock APIs.
#golang-tutorial #programming-languages #golang