![golang http client golang http client](https://app.gitbook.com/share/space/thumbnail/-M3uousqx7LBIEbZT--V.png)
Log.Println(resp.Code, string(resp.Data), err)ĬontentType: request. If you have experience about axios or requests, you will love it. It provides us with the Post method, allowing us to send data to a server.HTTP client for golang, Inspired by Javascript-axios Python-request. In Go, we can make POST request using the net/http package. This could create, deleting, or updating a resource. A HTTP method allows you to send data to the server and perform an action on the specified resource. The next part of a HTTP client is making POST method. The above code implements a HTTP client in Go capable of making HTTP GET request to the specified method.įor better readability, you can format the response into readable-json format or perform Marshal and Unmarshal to select the desired output.Ĭheck our tutorial on JSON Marshal and Unmarshal to learn more. Once we read the response body, we can print it out to the console as implemented in the code below:Ĭheck out our tutorial on Golang Defer to learn moreīody, err := ioutil. The () allows us to close the response body after reading. We can do this by setting the close method to run after the main function is finshed. NOTE: After reading the response Body, we need to close it to avoid if from locking other operations in your program. It returns the response body and an error. This method is defined in the ioutil package. We then need to read it and print it out to the console, or save it into a file.įor this, we can use the ioutil.ReadMe method. Also, with defer, you can make sure Body.Close () is called after all. So if you want to reuse TCP connections, you have to close Body every time after read to completion.
![golang http client golang http client](https://golangexample.com/content/images/2020/04/Request.png)
Hence, if we try to print it out, we will get a bunch of jumbled data.Īn example output from the above request is as shown below:įor us to view the actual data, we need to access the Body property on the struct returned. The default HTTP client's Transport may not // reuse HTTP/1.x 'keep-alive' TCP connections if the Body is // not read to completion and closed. Keep in mind that the resp contains nothing but a pointer to the struct holding the actual data.
![golang http client golang http client](https://static.admfactory.com/images/logos/golang-400.jpg)
In the example above, we call the Get method and pass the URL to the resource we wish to access as a string.Īs you will notice, the function returns two values which we store in two variables: resp and err which holds the returned response and the error, respectively. Import the net/http package by adding the line below in your go file.
![golang http client golang http client](https://golangexample.com/content/images/2020/12/gosip.png)
The net/http package provides us with the functionality and utilities to make HTTP request to remote servers. Once the project is created, open your main go file and import the net/http package from the go standard library. If you are not familiar with how to create and work with a go project, check our tutorial on Installing Go. The first step is to ensure you setup your project. For example, when we need to fetch articles from the server, we use a GET method. A GET method allows us to request data from a specified resource. The built-in features of Go already include a web server and the net/http package in the standard library, which. With Golang, web developers simply need the Go compiler and nothing else to get started developing web pages and web apps. The first method we are going to implement is HTTP GET method. The standard library support of Go, however, makes web development particularly easy, convenient, and quick. In this article, you will need the following resources: In this article, we will learn how to build a basic HTTP client using the Go programming language. Using HTTP request, we can request resources such as web pages, videos, images, and other files from a remote server. HTTP requests are one of the most building blocks of the modern web. The resource is mainly identified by an URL as a domain name or an IP address. Requests using GET should only retrieve data. A HTTP request refers to an action performed by an HTTP client on a specific HTTP (protocol) resource. Making a HTTP request in golang is pretty straightforward and simple, following are the examples by HTTP verbs GET The HTTP GET method requests a representation of the specified resource.