Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Go Programming - From Beginner to Professional - Second Edition

You're reading from  Go Programming - From Beginner to Professional - Second Edition

Product type Book
Published in Mar 2024
Publisher Packt
ISBN-13 9781803243054
Pages 680 pages
Edition 2nd Edition
Languages
Author (1):
Samantha Coyle Samantha Coyle
Profile icon Samantha Coyle

Table of Contents (30) Chapters

Preface Part 1: Scripts
Chapter 1: Variables and Operators Chapter 2: Command and Control Chapter 3: Core Types Chapter 4: Complex Types Part 2: Components
Chapter 5: Functions – Reduce, Reuse, and Recycle Chapter 6: Don’t Panic! Handle Your Errors Chapter 7: Interfaces Chapter 8: Generic Algorithm Superpowers Part 3: Modules
Chapter 9: Using Go Modules to Define a Project Chapter 10: Packages Keep Projects Manageable Chapter 11: Bug-Busting Debugging Skills Chapter 12: About Time Part 4: Applications
Chapter 13: Programming from the Command Line Chapter 14: File and Systems Chapter 15: SQL and Databases Part 5: Building For The Web
Chapter 16: Web Servers Chapter 17: Using the Go HTTP Client Part 6: Professional
Chapter 18: Concurrent Work Chapter 19: Testing Chapter 20: Using Go Tools Chapter 21: Go in the Cloud Index Other Books You May Enjoy

Using the Go HTTP Client

Overview

This chapter will equip you to use the Go HTTP Client to talk to other systems over the Internet.

You will start by learning to use the HTTP client to get data from a web server and to send data to a web server. By the end of the chapter, you will be able to upload a file to a web server and experiment with a custom Go HTTP Client to interact with web servers.

Technical requirements

For this chapter, you’ll require Go version 1.21 or higher. The code for this chapter can be found at: https://github.com/PacktPublishing/Go-Programming-From-Beginner-to-Professional-Second-Edition-/tree/main/Chapter17.

Introduction

In the previous chapter, you looked at web APIs with REST. You learned what REST is, how to think about API design, resources, and errors in REST, as well as the problems with REST and some of its alternatives.

In this chapter, you will learn about the Go HTTP Client and how to use it. An HTTP client is something that is used to get data from or send data to a web server. Probably the most well-known example of an HTTP client is a web browser (such as Firefox, Chrome, and Microsoft Edge). When you enter a web address into a web browser, it will have an HTTP client built in that sends a request to the server for data. The server will gather the data and send it back to the HTTP client, which will then display the web page in the browser. Similarly, when you fill out a form in a web browser, for example, when you log in to a website, the browser will use its HTTP client to send that form data to the server and then take appropriate action, depending on the response.

...

The Go HTTP Client and its uses

The Go HTTP Client is part of the Go standard library, specifically the net/http package. There are two main ways to use it. The first is to use the default HTTP client that is included in the net/http package. It’s simple to use and allows you to get up and running quickly. The second way is to create your own HTTP client based on the default HTTP client. This allows you to customize the requests and various other things. It takes longer to configure, but it gives you much more freedom and control over the requests you send.

When using an HTTP client, you can send different types of requests. While there are many types of requests, we will discuss the two main ones: the GET request and the POST request. For instance, if you wanted to retrieve data from a server, you would send a GET request. When you enter a web address in your web browser, it will send a GET request to the server at that address and then display the data it returns. If you...

Sending a request to a server

When you want to retrieve data from a web server, you send a GET request to the server. When sending a request, the URL will contain information on the resource from which you want data. The URL can be broken down into a few key parts. These include the protocol, the hostname, the URI, and the query parameters. The format of it looks like this:

Figure 17.1: URL format breakdown

Figure 17.1: URL format breakdown

We can see the following in this example:

  • Protocol tells the client how to connect to the server. The two most common protocols are HTTP and HTTPS. In this example, we have used https.
  • Hostname is the address of the server we want to connect to. In this example, it is example.com.
  • URI is the uniform resource identifier (URI), and this tells the server the path to the resource we want. In this example, it is /downloads.
  • Query Parameters tells the server about any additional information it needs. In this example, we have two query...

Structured data

Once you have requested data from a server, the data returned can come in various formats. For example, if you send a request to packtpub.com, it will return HTML data for the Packt website. While HTML data is useful for displaying websites, it isn’t ideal for sending machine-readable data. A common data type used in web APIs is JSON. JSON provides a good structure for data that is both machine-readable and human-readable. Later, you will learn how to parse JSON and make use of it using Go.

Exercise 17.02 – using the HTTP Client with structured data

In this exercise, you will parse structured JSON data in Go. The server will return JSON data, and you will use the json.Unmarshal function to parse the data and put them into a struct:

  1. Create a new directory, Exercise17.02. Within that directory, create two more directories, server and client. Then, within the server directory, create a file called server.go and write the following code:
    package...

Sending data to a server

In addition to requesting data from a server, you will also want to send data to a server. The most common way of doing this is via a POST request. A POST request comes in two main parts: the URL and the body. The body of a POST request is where you put the data you want to send to the server. A common example of this is a login form. When we send a login request, we POST the body to the URL. The web server then checks that the login details within the body are correct and updates our login status. It responds to the request by telling the client whether it succeeded or not. In this section, you will learn how to send data to a server using a POST request.

Exercise 17.03 – sending a POST request to a web server using the Go HTTP Client

In this exercise, you will send a POST request to a web server containing a message. The web server will then respond with the same message so you can confirm that it received it:

  1. Create a new directory,...

Summary

HTTP clients are used to interact with web servers. They are used to send different types of requests to a server (for example, GET or POST requests) and then react to the response returned by the server. A web browser is a type of HTTP client that will send a GET request to a web server and display the HTML data it returns. In Go, you created your own HTTP client and did the same thing, sending a GET request to https://www.google.com and then logging the response returned by the server. You also learned about the components of a URL and that you can control what you request from a server by changing the URL.

There is also more to web servers than simply requesting HTML data. You learned that they can return structured data in the form of JSON, which can be parsed and used in your code. Data can also be sent to a server using POST requests, allowing you to send form data to a server. However, the data sent to a server isn’t limited to just form data: you can also...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Go Programming - From Beginner to Professional - Second Edition
Published in: Mar 2024 Publisher: Packt ISBN-13: 9781803243054
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}